Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1 | /* Built-in functions */ |
| 2 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3 | #include "Python.h" |
Georg Brandl | fc8eef3 | 2008-03-28 12:11:56 +0000 | [diff] [blame] | 4 | #include "Python-ast.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 5 | |
| 6 | #include "node.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 7 | #include "code.h" |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 8 | #include "eval.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 10 | #include <ctype.h> |
| 11 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 12 | #ifdef RISCOS |
| 13 | #include "unixstuff.h" |
| 14 | #endif |
| 15 | |
Mark Hammond | 26cffde | 2001-05-14 12:17:34 +0000 | [diff] [blame] | 16 | /* The default encoding used by the platform file system APIs |
| 17 | Can remain NULL for all platforms that don't have such a concept |
| 18 | */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 19 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Mark Hammond | 26cffde | 2001-05-14 12:17:34 +0000 | [diff] [blame] | 20 | const char *Py_FileSystemDefaultEncoding = "mbcs"; |
Just van Rossum | b9b8e9c | 2003-02-10 09:22:01 +0000 | [diff] [blame] | 21 | #elif defined(__APPLE__) |
| 22 | const char *Py_FileSystemDefaultEncoding = "utf-8"; |
Mark Hammond | 26cffde | 2001-05-14 12:17:34 +0000 | [diff] [blame] | 23 | #else |
| 24 | const char *Py_FileSystemDefaultEncoding = NULL; /* use default */ |
| 25 | #endif |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 27 | /* Forward */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 28 | static PyObject *filterstring(PyObject *, PyObject *); |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 29 | #ifdef Py_USING_UNICODE |
| 30 | static PyObject *filterunicode(PyObject *, PyObject *); |
| 31 | #endif |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 32 | static PyObject *filtertuple (PyObject *, PyObject *); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 34 | static PyObject * |
Neal Norwitz | 92e212f | 2006-04-03 04:48:37 +0000 | [diff] [blame] | 35 | builtin___import__(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 36 | { |
Neal Norwitz | 92e212f | 2006-04-03 04:48:37 +0000 | [diff] [blame] | 37 | static char *kwlist[] = {"name", "globals", "locals", "fromlist", |
| 38 | "level", 0}; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 39 | char *name; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 40 | PyObject *globals = NULL; |
| 41 | PyObject *locals = NULL; |
| 42 | PyObject *fromlist = NULL; |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 43 | int level = -1; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 44 | |
Neal Norwitz | 92e212f | 2006-04-03 04:48:37 +0000 | [diff] [blame] | 45 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOi:__import__", |
| 46 | kwlist, &name, &globals, &locals, &fromlist, &level)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 47 | return NULL; |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 48 | return PyImport_ImportModuleLevel(name, globals, locals, |
| 49 | fromlist, level); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 52 | PyDoc_STRVAR(import_doc, |
Neal Norwitz | 92e212f | 2006-04-03 04:48:37 +0000 | [diff] [blame] | 53 | "__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 54 | \n\ |
| 55 | Import a module. The globals are only used to determine the context;\n\ |
| 56 | they are not modified. The locals are currently unused. The fromlist\n\ |
| 57 | should be a list of names to emulate ``from name import ...'', or an\n\ |
| 58 | empty list to emulate ``import name''.\n\ |
| 59 | When importing a module from a package, note that __import__('A.B', ...)\n\ |
| 60 | returns package A when fromlist is empty, but its submodule B when\n\ |
Neal Norwitz | 92e212f | 2006-04-03 04:48:37 +0000 | [diff] [blame] | 61 | fromlist is not empty. Level is used to determine whether to perform \n\ |
| 62 | absolute or relative imports. -1 is the original strategy of attempting\n\ |
| 63 | both absolute and relative imports, 0 is absolute, a positive number\n\ |
| 64 | is the number of parent directories to search relative to the current module."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 67 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 68 | builtin_abs(PyObject *self, PyObject *v) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 69 | { |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 70 | return PyNumber_Absolute(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 73 | PyDoc_STRVAR(abs_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 74 | "abs(number) -> number\n\ |
| 75 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 76 | Return the absolute value of the argument."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 77 | |
Raymond Hettinger | 96229b1 | 2005-03-11 06:49:40 +0000 | [diff] [blame] | 78 | static PyObject * |
| 79 | builtin_all(PyObject *self, PyObject *v) |
| 80 | { |
| 81 | PyObject *it, *item; |
Guido van Rossum | 01dbc10 | 2007-12-20 23:48:28 +0000 | [diff] [blame] | 82 | PyObject *(*iternext)(PyObject *); |
| 83 | int cmp; |
Raymond Hettinger | 96229b1 | 2005-03-11 06:49:40 +0000 | [diff] [blame] | 84 | |
| 85 | it = PyObject_GetIter(v); |
| 86 | if (it == NULL) |
| 87 | return NULL; |
Guido van Rossum | 01dbc10 | 2007-12-20 23:48:28 +0000 | [diff] [blame] | 88 | iternext = *Py_TYPE(it)->tp_iternext; |
Raymond Hettinger | 96229b1 | 2005-03-11 06:49:40 +0000 | [diff] [blame] | 89 | |
Guido van Rossum | 01dbc10 | 2007-12-20 23:48:28 +0000 | [diff] [blame] | 90 | for (;;) { |
| 91 | item = iternext(it); |
| 92 | if (item == NULL) |
| 93 | break; |
| 94 | cmp = PyObject_IsTrue(item); |
Raymond Hettinger | 96229b1 | 2005-03-11 06:49:40 +0000 | [diff] [blame] | 95 | Py_DECREF(item); |
| 96 | if (cmp < 0) { |
| 97 | Py_DECREF(it); |
| 98 | return NULL; |
| 99 | } |
| 100 | if (cmp == 0) { |
| 101 | Py_DECREF(it); |
| 102 | Py_RETURN_FALSE; |
| 103 | } |
| 104 | } |
| 105 | Py_DECREF(it); |
Guido van Rossum | 01dbc10 | 2007-12-20 23:48:28 +0000 | [diff] [blame] | 106 | if (PyErr_Occurred()) { |
| 107 | if (PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 108 | PyErr_Clear(); |
| 109 | else |
| 110 | return NULL; |
| 111 | } |
Raymond Hettinger | 96229b1 | 2005-03-11 06:49:40 +0000 | [diff] [blame] | 112 | Py_RETURN_TRUE; |
| 113 | } |
| 114 | |
| 115 | PyDoc_STRVAR(all_doc, |
| 116 | "all(iterable) -> bool\n\ |
| 117 | \n\ |
| 118 | Return True if bool(x) is True for all values x in the iterable."); |
| 119 | |
| 120 | static PyObject * |
| 121 | builtin_any(PyObject *self, PyObject *v) |
| 122 | { |
| 123 | PyObject *it, *item; |
Guido van Rossum | 01dbc10 | 2007-12-20 23:48:28 +0000 | [diff] [blame] | 124 | PyObject *(*iternext)(PyObject *); |
| 125 | int cmp; |
Raymond Hettinger | 96229b1 | 2005-03-11 06:49:40 +0000 | [diff] [blame] | 126 | |
| 127 | it = PyObject_GetIter(v); |
| 128 | if (it == NULL) |
| 129 | return NULL; |
Guido van Rossum | 01dbc10 | 2007-12-20 23:48:28 +0000 | [diff] [blame] | 130 | iternext = *Py_TYPE(it)->tp_iternext; |
Raymond Hettinger | 96229b1 | 2005-03-11 06:49:40 +0000 | [diff] [blame] | 131 | |
Guido van Rossum | 01dbc10 | 2007-12-20 23:48:28 +0000 | [diff] [blame] | 132 | for (;;) { |
| 133 | item = iternext(it); |
| 134 | if (item == NULL) |
| 135 | break; |
| 136 | cmp = PyObject_IsTrue(item); |
Raymond Hettinger | 96229b1 | 2005-03-11 06:49:40 +0000 | [diff] [blame] | 137 | Py_DECREF(item); |
| 138 | if (cmp < 0) { |
| 139 | Py_DECREF(it); |
| 140 | return NULL; |
| 141 | } |
| 142 | if (cmp == 1) { |
| 143 | Py_DECREF(it); |
| 144 | Py_RETURN_TRUE; |
| 145 | } |
| 146 | } |
| 147 | Py_DECREF(it); |
Guido van Rossum | 01dbc10 | 2007-12-20 23:48:28 +0000 | [diff] [blame] | 148 | if (PyErr_Occurred()) { |
| 149 | if (PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 150 | PyErr_Clear(); |
| 151 | else |
| 152 | return NULL; |
| 153 | } |
Raymond Hettinger | 96229b1 | 2005-03-11 06:49:40 +0000 | [diff] [blame] | 154 | Py_RETURN_FALSE; |
| 155 | } |
| 156 | |
| 157 | PyDoc_STRVAR(any_doc, |
| 158 | "any(iterable) -> bool\n\ |
| 159 | \n\ |
| 160 | Return True if bool(x) is True for any x in the iterable."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 161 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 162 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 163 | builtin_apply(PyObject *self, PyObject *args) |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 164 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 165 | PyObject *func, *alist = NULL, *kwdict = NULL; |
Barry Warsaw | 968f8cb | 1998-10-01 15:33:12 +0000 | [diff] [blame] | 166 | PyObject *t = NULL, *retval = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 167 | |
Benjamin Peterson | 9f4f481 | 2008-04-27 03:01:45 +0000 | [diff] [blame] | 168 | if (PyErr_WarnPy3k("apply() not supported in 3.x; " |
Benjamin Peterson | f19a7b9 | 2008-04-27 18:40:21 +0000 | [diff] [blame] | 169 | "use func(*args, **kwargs)", 1) < 0) |
Neal Norwitz | 8b2bfbc | 2007-05-23 06:35:32 +0000 | [diff] [blame] | 170 | return NULL; |
| 171 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 172 | if (!PyArg_UnpackTuple(args, "apply", 1, 3, &func, &alist, &kwdict)) |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 173 | return NULL; |
Barry Warsaw | 968f8cb | 1998-10-01 15:33:12 +0000 | [diff] [blame] | 174 | if (alist != NULL) { |
| 175 | if (!PyTuple_Check(alist)) { |
| 176 | if (!PySequence_Check(alist)) { |
Jeremy Hylton | c862cf4 | 2001-01-19 03:25:05 +0000 | [diff] [blame] | 177 | PyErr_Format(PyExc_TypeError, |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 178 | "apply() arg 2 expected sequence, found %s", |
Jeremy Hylton | c862cf4 | 2001-01-19 03:25:05 +0000 | [diff] [blame] | 179 | alist->ob_type->tp_name); |
Barry Warsaw | 968f8cb | 1998-10-01 15:33:12 +0000 | [diff] [blame] | 180 | return NULL; |
| 181 | } |
| 182 | t = PySequence_Tuple(alist); |
| 183 | if (t == NULL) |
| 184 | return NULL; |
| 185 | alist = t; |
| 186 | } |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 187 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 188 | if (kwdict != NULL && !PyDict_Check(kwdict)) { |
Jeremy Hylton | c862cf4 | 2001-01-19 03:25:05 +0000 | [diff] [blame] | 189 | PyErr_Format(PyExc_TypeError, |
| 190 | "apply() arg 3 expected dictionary, found %s", |
| 191 | kwdict->ob_type->tp_name); |
Barry Warsaw | 968f8cb | 1998-10-01 15:33:12 +0000 | [diff] [blame] | 192 | goto finally; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 193 | } |
Barry Warsaw | 968f8cb | 1998-10-01 15:33:12 +0000 | [diff] [blame] | 194 | retval = PyEval_CallObjectWithKeywords(func, alist, kwdict); |
| 195 | finally: |
| 196 | Py_XDECREF(t); |
| 197 | return retval; |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 200 | PyDoc_STRVAR(apply_doc, |
Fred Drake | f1fbc62 | 2001-01-12 17:05:05 +0000 | [diff] [blame] | 201 | "apply(object[, args[, kwargs]]) -> value\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 202 | \n\ |
Fred Drake | 7b91212 | 1999-12-23 14:16:55 +0000 | [diff] [blame] | 203 | Call a callable object with positional arguments taken from the tuple args,\n\ |
| 204 | and keyword arguments taken from the optional dictionary kwargs.\n\ |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 205 | Note that classes are callable, as are instances with a __call__() method.\n\ |
| 206 | \n\ |
| 207 | Deprecated since release 2.3. Instead, use the extended call syntax:\n\ |
| 208 | function(*args, **keywords)."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 209 | |
| 210 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 211 | static PyObject * |
Eric Smith | 3cd8194 | 2008-02-22 16:30:22 +0000 | [diff] [blame] | 212 | builtin_bin(PyObject *self, PyObject *v) |
| 213 | { |
Raymond Hettinger | e3ae655 | 2008-06-20 04:18:15 +0000 | [diff] [blame^] | 214 | PyNumberMethods *nb; |
| 215 | PyObject *res; |
| 216 | |
| 217 | if ((nb = v->ob_type->tp_as_number) == NULL || |
| 218 | nb->nb_hex == NULL) { |
| 219 | PyErr_SetString(PyExc_TypeError, |
| 220 | "bin() argument can't be converted to hex"); |
| 221 | return NULL; |
| 222 | } |
| 223 | res = (*nb->nb_bin)(v); |
| 224 | if (res && !PyString_Check(res)) { |
| 225 | PyErr_Format(PyExc_TypeError, |
| 226 | "__bin__ returned non-string (type %.200s)", |
| 227 | res->ob_type->tp_name); |
| 228 | Py_DECREF(res); |
| 229 | return NULL; |
| 230 | } |
| 231 | return res; |
Eric Smith | 3cd8194 | 2008-02-22 16:30:22 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | PyDoc_STRVAR(bin_doc, |
| 235 | "bin(number) -> string\n\ |
| 236 | \n\ |
| 237 | Return the binary representation of an integer or long integer."); |
| 238 | |
| 239 | |
| 240 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 241 | builtin_callable(PyObject *self, PyObject *v) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 242 | { |
Benjamin Peterson | 9f4f481 | 2008-04-27 03:01:45 +0000 | [diff] [blame] | 243 | if (PyErr_WarnPy3k("callable() not supported in 3.x; " |
Benjamin Peterson | f19a7b9 | 2008-04-27 18:40:21 +0000 | [diff] [blame] | 244 | "use hasattr(o, '__call__')", 1) < 0) |
Neal Norwitz | df25efe | 2007-05-23 06:58:36 +0000 | [diff] [blame] | 245 | return NULL; |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 246 | return PyBool_FromLong((long)PyCallable_Check(v)); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 249 | PyDoc_STRVAR(callable_doc, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 250 | "callable(object) -> bool\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 251 | \n\ |
| 252 | Return whether the object is callable (i.e., some kind of function).\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 253 | Note that classes are callable, as are instances with a __call__() method."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 254 | |
| 255 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 256 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 257 | builtin_filter(PyObject *self, PyObject *args) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 258 | { |
Guido van Rossum | c7903a1 | 2002-08-16 07:04:56 +0000 | [diff] [blame] | 259 | PyObject *func, *seq, *result, *it, *arg; |
Martin v. Löwis | d96ee90 | 2006-02-16 14:37:16 +0000 | [diff] [blame] | 260 | Py_ssize_t len; /* guess for result list size */ |
| 261 | register Py_ssize_t j; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 262 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 263 | if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq)) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 264 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 265 | |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 266 | /* Strings and tuples return a result of the same type. */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 267 | if (PyString_Check(seq)) |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 268 | return filterstring(func, seq); |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 269 | #ifdef Py_USING_UNICODE |
| 270 | if (PyUnicode_Check(seq)) |
| 271 | return filterunicode(func, seq); |
| 272 | #endif |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 273 | if (PyTuple_Check(seq)) |
| 274 | return filtertuple(func, seq); |
| 275 | |
Georg Brandl | e35b657 | 2005-07-19 22:20:20 +0000 | [diff] [blame] | 276 | /* Pre-allocate argument list tuple. */ |
| 277 | arg = PyTuple_New(1); |
| 278 | if (arg == NULL) |
| 279 | return NULL; |
| 280 | |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 281 | /* Get iterator. */ |
| 282 | it = PyObject_GetIter(seq); |
| 283 | if (it == NULL) |
Georg Brandl | e35b657 | 2005-07-19 22:20:20 +0000 | [diff] [blame] | 284 | goto Fail_arg; |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 285 | |
| 286 | /* Guess a result list size. */ |
Raymond Hettinger | 4e2f714 | 2007-12-06 00:56:53 +0000 | [diff] [blame] | 287 | len = _PyObject_LengthHint(seq, 8); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 288 | |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 289 | /* Get a result list. */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 290 | if (PyList_Check(seq) && seq->ob_refcnt == 1) { |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 291 | /* Eww - can modify the list in-place. */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 292 | Py_INCREF(seq); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 293 | result = seq; |
| 294 | } |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 295 | else { |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 296 | result = PyList_New(len); |
| 297 | if (result == NULL) |
| 298 | goto Fail_it; |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 299 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 300 | |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 301 | /* Build the result list. */ |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 302 | j = 0; |
| 303 | for (;;) { |
Guido van Rossum | c7903a1 | 2002-08-16 07:04:56 +0000 | [diff] [blame] | 304 | PyObject *item; |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 305 | int ok; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 306 | |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 307 | item = PyIter_Next(it); |
| 308 | if (item == NULL) { |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 309 | if (PyErr_Occurred()) |
| 310 | goto Fail_result_it; |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 311 | break; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 312 | } |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 313 | |
Neil Schemenauer | 6897355 | 2003-08-14 20:37:34 +0000 | [diff] [blame] | 314 | if (func == (PyObject *)&PyBool_Type || func == Py_None) { |
Guido van Rossum | c7903a1 | 2002-08-16 07:04:56 +0000 | [diff] [blame] | 315 | ok = PyObject_IsTrue(item); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 316 | } |
| 317 | else { |
Guido van Rossum | c7903a1 | 2002-08-16 07:04:56 +0000 | [diff] [blame] | 318 | PyObject *good; |
| 319 | PyTuple_SET_ITEM(arg, 0, item); |
| 320 | good = PyObject_Call(func, arg, NULL); |
| 321 | PyTuple_SET_ITEM(arg, 0, NULL); |
Guido van Rossum | 58b6873 | 1995-01-10 17:40:55 +0000 | [diff] [blame] | 322 | if (good == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 323 | Py_DECREF(item); |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 324 | goto Fail_result_it; |
Guido van Rossum | 58b6873 | 1995-01-10 17:40:55 +0000 | [diff] [blame] | 325 | } |
Guido van Rossum | c7903a1 | 2002-08-16 07:04:56 +0000 | [diff] [blame] | 326 | ok = PyObject_IsTrue(good); |
| 327 | Py_DECREF(good); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 328 | } |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 329 | if (ok) { |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 330 | if (j < len) |
| 331 | PyList_SET_ITEM(result, j, item); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 332 | else { |
Barry Warsaw | fa77e09 | 1999-01-28 18:49:12 +0000 | [diff] [blame] | 333 | int status = PyList_Append(result, item); |
Barry Warsaw | fa77e09 | 1999-01-28 18:49:12 +0000 | [diff] [blame] | 334 | Py_DECREF(item); |
| 335 | if (status < 0) |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 336 | goto Fail_result_it; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 337 | } |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 338 | ++j; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 339 | } |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 340 | else |
| 341 | Py_DECREF(item); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 344 | |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 345 | /* Cut back result list if len is too big. */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 346 | if (j < len && PyList_SetSlice(result, j, len, NULL) < 0) |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 347 | goto Fail_result_it; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 348 | |
Tim Peters | 3c6b148 | 2001-05-21 08:07:05 +0000 | [diff] [blame] | 349 | Py_DECREF(it); |
Guido van Rossum | c7903a1 | 2002-08-16 07:04:56 +0000 | [diff] [blame] | 350 | Py_DECREF(arg); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 351 | return result; |
| 352 | |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 353 | Fail_result_it: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 354 | Py_DECREF(result); |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 355 | Fail_it: |
| 356 | Py_DECREF(it); |
Guido van Rossum | c7903a1 | 2002-08-16 07:04:56 +0000 | [diff] [blame] | 357 | Fail_arg: |
| 358 | Py_DECREF(arg); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 359 | return NULL; |
| 360 | } |
| 361 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 362 | PyDoc_STRVAR(filter_doc, |
Tim Peters | d50e544 | 2002-03-09 00:06:26 +0000 | [diff] [blame] | 363 | "filter(function or None, sequence) -> list, tuple, or string\n" |
| 364 | "\n" |
| 365 | "Return those items of sequence for which function(item) is true. If\n" |
| 366 | "function is None, return the items that are true. If sequence is a tuple\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 367 | "or string, return the same type, else return a list."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 368 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 369 | static PyObject * |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 370 | builtin_format(PyObject *self, PyObject *args) |
| 371 | { |
| 372 | PyObject *value; |
| 373 | PyObject *format_spec = NULL; |
| 374 | |
| 375 | if (!PyArg_ParseTuple(args, "O|O:format", &value, &format_spec)) |
| 376 | return NULL; |
| 377 | |
| 378 | return PyObject_Format(value, format_spec); |
| 379 | } |
| 380 | |
| 381 | PyDoc_STRVAR(format_doc, |
| 382 | "format(value[, format_spec]) -> string\n\ |
| 383 | \n\ |
| 384 | Returns value.__format__(format_spec)\n\ |
| 385 | format_spec defaults to \"\""); |
| 386 | |
| 387 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 388 | builtin_chr(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 389 | { |
| 390 | long x; |
| 391 | char s[1]; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 392 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 393 | if (!PyArg_ParseTuple(args, "l:chr", &x)) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 394 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 395 | if (x < 0 || x >= 256) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 396 | PyErr_SetString(PyExc_ValueError, |
| 397 | "chr() arg not in range(256)"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 398 | return NULL; |
| 399 | } |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 400 | s[0] = (char)x; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 401 | return PyString_FromStringAndSize(s, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 404 | PyDoc_STRVAR(chr_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 405 | "chr(i) -> character\n\ |
| 406 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 407 | Return a string of one character with ordinal i; 0 <= i < 256."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 408 | |
| 409 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 410 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 411 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 412 | builtin_unichr(PyObject *self, PyObject *args) |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 413 | { |
| 414 | long x; |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 415 | |
| 416 | if (!PyArg_ParseTuple(args, "l:unichr", &x)) |
| 417 | return NULL; |
Fredrik Lundh | 0dcf67e | 2001-06-26 20:01:56 +0000 | [diff] [blame] | 418 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 419 | return PyUnicode_FromOrdinal(x); |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 422 | PyDoc_STRVAR(unichr_doc, |
Fred Drake | 078b24f | 2000-04-13 02:42:50 +0000 | [diff] [blame] | 423 | "unichr(i) -> Unicode character\n\ |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 424 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 425 | Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff."); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 426 | #endif |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 427 | |
| 428 | |
| 429 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 430 | builtin_cmp(PyObject *self, PyObject *args) |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 431 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 432 | PyObject *a, *b; |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 433 | int c; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 434 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 435 | if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b)) |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 436 | return NULL; |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 437 | if (PyObject_Cmp(a, b, &c) < 0) |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 438 | return NULL; |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 439 | return PyInt_FromLong((long)c); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 442 | PyDoc_STRVAR(cmp_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 443 | "cmp(x, y) -> integer\n\ |
| 444 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 445 | Return negative if x<y, zero if x==y, positive if x>y."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 446 | |
| 447 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 448 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 449 | builtin_coerce(PyObject *self, PyObject *args) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 450 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 451 | PyObject *v, *w; |
| 452 | PyObject *res; |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 453 | |
Benjamin Peterson | 9f4f481 | 2008-04-27 03:01:45 +0000 | [diff] [blame] | 454 | if (PyErr_WarnPy3k("coerce() not supported in 3.x", 1) < 0) |
Neal Norwitz | df25efe | 2007-05-23 06:58:36 +0000 | [diff] [blame] | 455 | return NULL; |
| 456 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 457 | if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w)) |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 458 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 459 | if (PyNumber_Coerce(&v, &w) < 0) |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 460 | return NULL; |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 461 | res = PyTuple_Pack(2, v, w); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 462 | Py_DECREF(v); |
| 463 | Py_DECREF(w); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 464 | return res; |
| 465 | } |
| 466 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 467 | PyDoc_STRVAR(coerce_doc, |
Martin v. Löwis | 8d494f3 | 2004-08-25 10:42:41 +0000 | [diff] [blame] | 468 | "coerce(x, y) -> (x1, y1)\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 469 | \n\ |
Martin v. Löwis | 8d494f3 | 2004-08-25 10:42:41 +0000 | [diff] [blame] | 470 | Return a tuple consisting of the two numeric arguments converted to\n\ |
| 471 | a common type, using the same rules as used by arithmetic operations.\n\ |
| 472 | If coercion is not possible, raise TypeError."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 473 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 474 | static PyObject * |
Georg Brandl | 5240d74 | 2007-03-13 20:46:32 +0000 | [diff] [blame] | 475 | builtin_compile(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 476 | { |
| 477 | char *str; |
| 478 | char *filename; |
| 479 | char *startstr; |
Georg Brandl | f2bfd54 | 2008-03-29 13:24:23 +0000 | [diff] [blame] | 480 | int mode = -1; |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 481 | int dont_inherit = 0; |
| 482 | int supplied_flags = 0; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 483 | PyCompilerFlags cf; |
Neal Norwitz | 3a9a3e7 | 2005-11-27 20:38:31 +0000 | [diff] [blame] | 484 | PyObject *result = NULL, *cmd, *tmp = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 485 | Py_ssize_t length; |
Georg Brandl | 5240d74 | 2007-03-13 20:46:32 +0000 | [diff] [blame] | 486 | static char *kwlist[] = {"source", "filename", "mode", "flags", |
| 487 | "dont_inherit", NULL}; |
Georg Brandl | f2bfd54 | 2008-03-29 13:24:23 +0000 | [diff] [blame] | 488 | int start[] = {Py_file_input, Py_eval_input, Py_single_input}; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 489 | |
Georg Brandl | 5240d74 | 2007-03-13 20:46:32 +0000 | [diff] [blame] | 490 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile", |
| 491 | kwlist, &cmd, &filename, &startstr, |
| 492 | &supplied_flags, &dont_inherit)) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 493 | return NULL; |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 494 | |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 495 | cf.cf_flags = supplied_flags; |
| 496 | |
Georg Brandl | fc8eef3 | 2008-03-28 12:11:56 +0000 | [diff] [blame] | 497 | if (supplied_flags & |
| 498 | ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST)) |
| 499 | { |
| 500 | PyErr_SetString(PyExc_ValueError, |
| 501 | "compile(): unrecognised flags"); |
| 502 | return NULL; |
| 503 | } |
| 504 | /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */ |
| 505 | |
| 506 | if (!dont_inherit) { |
| 507 | PyEval_MergeCompilerFlags(&cf); |
| 508 | } |
| 509 | |
Georg Brandl | f2bfd54 | 2008-03-29 13:24:23 +0000 | [diff] [blame] | 510 | if (strcmp(startstr, "exec") == 0) |
| 511 | mode = 0; |
| 512 | else if (strcmp(startstr, "eval") == 0) |
| 513 | mode = 1; |
| 514 | else if (strcmp(startstr, "single") == 0) |
| 515 | mode = 2; |
| 516 | else { |
| 517 | PyErr_SetString(PyExc_ValueError, |
| 518 | "compile() arg 3 must be 'exec', 'eval' or 'single'"); |
| 519 | return NULL; |
| 520 | } |
| 521 | |
Georg Brandl | fc8eef3 | 2008-03-28 12:11:56 +0000 | [diff] [blame] | 522 | if (PyAST_Check(cmd)) { |
| 523 | if (supplied_flags & PyCF_ONLY_AST) { |
| 524 | Py_INCREF(cmd); |
| 525 | result = cmd; |
| 526 | } |
| 527 | else { |
| 528 | PyArena *arena; |
| 529 | mod_ty mod; |
| 530 | |
| 531 | arena = PyArena_New(); |
Georg Brandl | f2bfd54 | 2008-03-29 13:24:23 +0000 | [diff] [blame] | 532 | mod = PyAST_obj2mod(cmd, arena, mode); |
Georg Brandl | fc8eef3 | 2008-03-28 12:11:56 +0000 | [diff] [blame] | 533 | if (mod == NULL) { |
| 534 | PyArena_Free(arena); |
| 535 | return NULL; |
| 536 | } |
| 537 | result = (PyObject*)PyAST_Compile(mod, filename, |
| 538 | &cf, arena); |
| 539 | PyArena_Free(arena); |
| 540 | } |
| 541 | return result; |
| 542 | } |
| 543 | |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 544 | #ifdef Py_USING_UNICODE |
| 545 | if (PyUnicode_Check(cmd)) { |
| 546 | tmp = PyUnicode_AsUTF8String(cmd); |
| 547 | if (tmp == NULL) |
| 548 | return NULL; |
| 549 | cmd = tmp; |
| 550 | cf.cf_flags |= PyCF_SOURCE_IS_UTF8; |
| 551 | } |
| 552 | #endif |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 553 | |
Georg Brandl | fc8eef3 | 2008-03-28 12:11:56 +0000 | [diff] [blame] | 554 | if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length)) |
Neal Norwitz | 3a9a3e7 | 2005-11-27 20:38:31 +0000 | [diff] [blame] | 555 | goto cleanup; |
Georg Brandl | fc8eef3 | 2008-03-28 12:11:56 +0000 | [diff] [blame] | 556 | if ((size_t)length != strlen(str)) { |
| 557 | PyErr_SetString(PyExc_TypeError, |
| 558 | "compile() expected string without null bytes"); |
| 559 | goto cleanup; |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 560 | } |
Georg Brandl | f2bfd54 | 2008-03-29 13:24:23 +0000 | [diff] [blame] | 561 | result = Py_CompileStringFlags(str, filename, start[mode], &cf); |
Neal Norwitz | 3a9a3e7 | 2005-11-27 20:38:31 +0000 | [diff] [blame] | 562 | cleanup: |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 563 | Py_XDECREF(tmp); |
| 564 | return result; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 567 | PyDoc_STRVAR(compile_doc, |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 568 | "compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 569 | \n\ |
| 570 | Compile the source string (a Python module, statement or expression)\n\ |
| 571 | into a code object that can be executed by the exec statement or eval().\n\ |
| 572 | The filename will be used for run-time error messages.\n\ |
| 573 | The mode must be 'exec' to compile a module, 'single' to compile a\n\ |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 574 | single (interactive) statement, or 'eval' to compile an expression.\n\ |
| 575 | The flags argument, if present, controls which future statements influence\n\ |
| 576 | the compilation of the code.\n\ |
| 577 | The dont_inherit argument, if non-zero, stops the compilation inheriting\n\ |
| 578 | the effects of any future statements in effect in the code calling\n\ |
| 579 | compile; if absent or zero these statements do influence the compilation,\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 580 | in addition to any features explicitly specified."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 581 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 582 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 583 | builtin_dir(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 584 | { |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 585 | PyObject *arg = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 586 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 587 | if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 588 | return NULL; |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 589 | return PyObject_Dir(arg); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 592 | PyDoc_STRVAR(dir_doc, |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 593 | "dir([object]) -> list of strings\n" |
| 594 | "\n" |
Georg Brandl | 871f1bc | 2007-03-12 13:17:36 +0000 | [diff] [blame] | 595 | "If called without an argument, return the names in the current scope.\n" |
| 596 | "Else, return an alphabetized list of names comprising (some of) the attributes\n" |
| 597 | "of the given object, and of attributes reachable from it.\n" |
| 598 | "If the object supplies a method named __dir__, it will be used; otherwise\n" |
| 599 | "the default dir() logic is used and returns:\n" |
| 600 | " for a module object: the module's attributes.\n" |
| 601 | " for a class object: its attributes, and recursively the attributes\n" |
| 602 | " of its bases.\n" |
Georg Brandl | 3bb1567 | 2007-03-13 07:23:16 +0000 | [diff] [blame] | 603 | " for any other object: its attributes, its class's attributes, and\n" |
Georg Brandl | 871f1bc | 2007-03-12 13:17:36 +0000 | [diff] [blame] | 604 | " recursively the attributes of its class's base classes."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 605 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 606 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 607 | builtin_divmod(PyObject *self, PyObject *args) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 608 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 609 | PyObject *v, *w; |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 610 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 611 | if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w)) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 612 | return NULL; |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 613 | return PyNumber_Divmod(v, w); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 616 | PyDoc_STRVAR(divmod_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 617 | "divmod(x, y) -> (div, mod)\n\ |
| 618 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 619 | Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 620 | |
| 621 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 622 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 623 | builtin_eval(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 624 | { |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 625 | PyObject *cmd, *result, *tmp = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 626 | PyObject *globals = Py_None, *locals = Py_None; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 627 | char *str; |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 628 | PyCompilerFlags cf; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 629 | |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 630 | if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 631 | return NULL; |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 632 | if (locals != Py_None && !PyMapping_Check(locals)) { |
| 633 | PyErr_SetString(PyExc_TypeError, "locals must be a mapping"); |
Raymond Hettinger | 513ffe8 | 2004-07-06 13:44:41 +0000 | [diff] [blame] | 634 | return NULL; |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 635 | } |
| 636 | if (globals != Py_None && !PyDict_Check(globals)) { |
Georg Brandl | 99d7e4e | 2005-08-31 22:21:15 +0000 | [diff] [blame] | 637 | PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ? |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 638 | "globals must be a real dict; try eval(expr, {}, mapping)" |
| 639 | : "globals must be a dict"); |
Raymond Hettinger | 513ffe8 | 2004-07-06 13:44:41 +0000 | [diff] [blame] | 640 | return NULL; |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 641 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 642 | if (globals == Py_None) { |
| 643 | globals = PyEval_GetGlobals(); |
| 644 | if (locals == Py_None) |
| 645 | locals = PyEval_GetLocals(); |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 646 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 647 | else if (locals == Py_None) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 648 | locals = globals; |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 649 | |
Georg Brandl | 77c85e6 | 2005-09-15 10:46:13 +0000 | [diff] [blame] | 650 | if (globals == NULL || locals == NULL) { |
| 651 | PyErr_SetString(PyExc_TypeError, |
| 652 | "eval must be given globals and locals " |
| 653 | "when called without a frame"); |
| 654 | return NULL; |
| 655 | } |
| 656 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 657 | if (PyDict_GetItemString(globals, "__builtins__") == NULL) { |
| 658 | if (PyDict_SetItemString(globals, "__builtins__", |
| 659 | PyEval_GetBuiltins()) != 0) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 660 | return NULL; |
| 661 | } |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 662 | |
Jeremy Hylton | 15c1c4f | 2001-07-30 21:50:55 +0000 | [diff] [blame] | 663 | if (PyCode_Check(cmd)) { |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 664 | if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) { |
Jeremy Hylton | 15c1c4f | 2001-07-30 21:50:55 +0000 | [diff] [blame] | 665 | PyErr_SetString(PyExc_TypeError, |
| 666 | "code object passed to eval() may not contain free variables"); |
| 667 | return NULL; |
| 668 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 669 | return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals); |
Jeremy Hylton | 15c1c4f | 2001-07-30 21:50:55 +0000 | [diff] [blame] | 670 | } |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 671 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 672 | if (!PyString_Check(cmd) && |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 673 | !PyUnicode_Check(cmd)) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 674 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 675 | "eval() arg 1 must be a string or code object"); |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 676 | return NULL; |
| 677 | } |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 678 | cf.cf_flags = 0; |
| 679 | |
| 680 | #ifdef Py_USING_UNICODE |
| 681 | if (PyUnicode_Check(cmd)) { |
| 682 | tmp = PyUnicode_AsUTF8String(cmd); |
| 683 | if (tmp == NULL) |
| 684 | return NULL; |
| 685 | cmd = tmp; |
| 686 | cf.cf_flags |= PyCF_SOURCE_IS_UTF8; |
| 687 | } |
| 688 | #endif |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 689 | if (PyString_AsStringAndSize(cmd, &str, NULL)) { |
Neal Norwitz | 3a9a3e7 | 2005-11-27 20:38:31 +0000 | [diff] [blame] | 690 | Py_XDECREF(tmp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 691 | return NULL; |
Neal Norwitz | 3a9a3e7 | 2005-11-27 20:38:31 +0000 | [diff] [blame] | 692 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 693 | while (*str == ' ' || *str == '\t') |
| 694 | str++; |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 695 | |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 696 | (void)PyEval_MergeCompilerFlags(&cf); |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 697 | result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf); |
| 698 | Py_XDECREF(tmp); |
| 699 | return result; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 702 | PyDoc_STRVAR(eval_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 703 | "eval(source[, globals[, locals]]) -> value\n\ |
| 704 | \n\ |
| 705 | Evaluate the source in the context of globals and locals.\n\ |
| 706 | The source may be a string representing a Python expression\n\ |
| 707 | or a code object as returned by compile().\n\ |
Neal Norwitz | 477ca1c | 2006-09-05 02:25:41 +0000 | [diff] [blame] | 708 | The globals must be a dictionary and locals can be any mapping,\n\ |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 709 | defaulting to the current globals and locals.\n\ |
| 710 | If only globals is given, locals defaults to it.\n"); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 711 | |
| 712 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 713 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 714 | builtin_execfile(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 715 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 716 | char *filename; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 717 | PyObject *globals = Py_None, *locals = Py_None; |
| 718 | PyObject *res; |
Guido van Rossum | b3a639e | 2001-09-05 13:37:47 +0000 | [diff] [blame] | 719 | FILE* fp = NULL; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 720 | PyCompilerFlags cf; |
Martin v. Löwis | 6b3a2c4 | 2001-08-08 05:30:36 +0000 | [diff] [blame] | 721 | int exists; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 722 | |
Benjamin Peterson | 9f4f481 | 2008-04-27 03:01:45 +0000 | [diff] [blame] | 723 | if (PyErr_WarnPy3k("execfile() not supported in 3.x; use exec()", |
Benjamin Peterson | f19a7b9 | 2008-04-27 18:40:21 +0000 | [diff] [blame] | 724 | 1) < 0) |
Neal Norwitz | df25efe | 2007-05-23 06:58:36 +0000 | [diff] [blame] | 725 | return NULL; |
| 726 | |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 727 | if (!PyArg_ParseTuple(args, "s|O!O:execfile", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 728 | &filename, |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 729 | &PyDict_Type, &globals, |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 730 | &locals)) |
Guido van Rossum | 0f61f8a | 1992-02-25 18:55:05 +0000 | [diff] [blame] | 731 | return NULL; |
Raymond Hettinger | 66bd233 | 2004-08-02 08:30:07 +0000 | [diff] [blame] | 732 | if (locals != Py_None && !PyMapping_Check(locals)) { |
| 733 | PyErr_SetString(PyExc_TypeError, "locals must be a mapping"); |
| 734 | return NULL; |
| 735 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 736 | if (globals == Py_None) { |
| 737 | globals = PyEval_GetGlobals(); |
| 738 | if (locals == Py_None) |
| 739 | locals = PyEval_GetLocals(); |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 740 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 741 | else if (locals == Py_None) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 742 | locals = globals; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 743 | if (PyDict_GetItemString(globals, "__builtins__") == NULL) { |
| 744 | if (PyDict_SetItemString(globals, "__builtins__", |
| 745 | PyEval_GetBuiltins()) != 0) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 746 | return NULL; |
| 747 | } |
Martin v. Löwis | 6b3a2c4 | 2001-08-08 05:30:36 +0000 | [diff] [blame] | 748 | |
| 749 | exists = 0; |
| 750 | /* Test for existence or directory. */ |
Martin v. Löwis | 3484a18 | 2002-03-09 12:07:51 +0000 | [diff] [blame] | 751 | #if defined(PLAN9) |
| 752 | { |
| 753 | Dir *d; |
| 754 | |
| 755 | if ((d = dirstat(filename))!=nil) { |
| 756 | if(d->mode & DMDIR) |
| 757 | werrstr("is a directory"); |
| 758 | else |
| 759 | exists = 1; |
| 760 | free(d); |
| 761 | } |
Martin v. Löwis | 6b3a2c4 | 2001-08-08 05:30:36 +0000 | [diff] [blame] | 762 | } |
Martin v. Löwis | 3484a18 | 2002-03-09 12:07:51 +0000 | [diff] [blame] | 763 | #elif defined(RISCOS) |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 764 | if (object_exists(filename)) { |
| 765 | if (isdir(filename)) |
| 766 | errno = EISDIR; |
| 767 | else |
| 768 | exists = 1; |
| 769 | } |
Martin v. Löwis | 3484a18 | 2002-03-09 12:07:51 +0000 | [diff] [blame] | 770 | #else /* standard Posix */ |
| 771 | { |
| 772 | struct stat s; |
| 773 | if (stat(filename, &s) == 0) { |
| 774 | if (S_ISDIR(s.st_mode)) |
Andrew MacIntyre | da4d6cb | 2004-03-29 11:53:38 +0000 | [diff] [blame] | 775 | # if defined(PYOS_OS2) && defined(PYCC_VACPP) |
Martin v. Löwis | 3484a18 | 2002-03-09 12:07:51 +0000 | [diff] [blame] | 776 | errno = EOS2ERR; |
| 777 | # else |
| 778 | errno = EISDIR; |
| 779 | # endif |
| 780 | else |
| 781 | exists = 1; |
| 782 | } |
| 783 | } |
| 784 | #endif |
Martin v. Löwis | 6b3a2c4 | 2001-08-08 05:30:36 +0000 | [diff] [blame] | 785 | |
| 786 | if (exists) { |
| 787 | Py_BEGIN_ALLOW_THREADS |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 788 | fp = fopen(filename, "r" PY_STDIOTEXTMODE); |
Martin v. Löwis | 6b3a2c4 | 2001-08-08 05:30:36 +0000 | [diff] [blame] | 789 | Py_END_ALLOW_THREADS |
| 790 | |
| 791 | if (fp == NULL) { |
| 792 | exists = 0; |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | if (!exists) { |
Peter Schneider-Kamp | 4c01342 | 2002-08-27 16:58:00 +0000 | [diff] [blame] | 797 | PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename); |
Guido van Rossum | 0f61f8a | 1992-02-25 18:55:05 +0000 | [diff] [blame] | 798 | return NULL; |
| 799 | } |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 800 | cf.cf_flags = 0; |
| 801 | if (PyEval_MergeCompilerFlags(&cf)) |
Tim Peters | 748b8bb | 2001-04-28 08:20:22 +0000 | [diff] [blame] | 802 | res = PyRun_FileExFlags(fp, filename, Py_file_input, globals, |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 803 | locals, 1, &cf); |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 804 | else |
Tim Peters | 748b8bb | 2001-04-28 08:20:22 +0000 | [diff] [blame] | 805 | res = PyRun_FileEx(fp, filename, Py_file_input, globals, |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 806 | locals, 1); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 807 | return res; |
Guido van Rossum | 0f61f8a | 1992-02-25 18:55:05 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 810 | PyDoc_STRVAR(execfile_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 811 | "execfile(filename[, globals[, locals]])\n\ |
| 812 | \n\ |
| 813 | Read and execute a Python script from a file.\n\ |
| 814 | The globals and locals are dictionaries, defaulting to the current\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 815 | globals and locals. If only globals is given, locals defaults to it."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 816 | |
| 817 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 818 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 819 | builtin_getattr(PyObject *self, PyObject *args) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 820 | { |
Guido van Rossum | 950ff29 | 1998-06-29 13:38:57 +0000 | [diff] [blame] | 821 | PyObject *v, *result, *dflt = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 822 | PyObject *name; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 823 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 824 | if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt)) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 825 | return NULL; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 826 | #ifdef Py_USING_UNICODE |
Jeremy Hylton | 0eb1115 | 2001-07-30 22:39:31 +0000 | [diff] [blame] | 827 | if (PyUnicode_Check(name)) { |
| 828 | name = _PyUnicode_AsDefaultEncodedString(name, NULL); |
| 829 | if (name == NULL) |
| 830 | return NULL; |
| 831 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 832 | #endif |
Jeremy Hylton | 0eb1115 | 2001-07-30 22:39:31 +0000 | [diff] [blame] | 833 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 834 | if (!PyString_Check(name)) { |
Jeremy Hylton | 0eb1115 | 2001-07-30 22:39:31 +0000 | [diff] [blame] | 835 | PyErr_SetString(PyExc_TypeError, |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 836 | "getattr(): attribute name must be string"); |
Jeremy Hylton | 0eb1115 | 2001-07-30 22:39:31 +0000 | [diff] [blame] | 837 | return NULL; |
| 838 | } |
Guido van Rossum | 950ff29 | 1998-06-29 13:38:57 +0000 | [diff] [blame] | 839 | result = PyObject_GetAttr(v, name); |
Guido van Rossum | d892357 | 2001-10-16 21:31:32 +0000 | [diff] [blame] | 840 | if (result == NULL && dflt != NULL && |
| 841 | PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 842 | { |
Guido van Rossum | 950ff29 | 1998-06-29 13:38:57 +0000 | [diff] [blame] | 843 | PyErr_Clear(); |
| 844 | Py_INCREF(dflt); |
| 845 | result = dflt; |
| 846 | } |
| 847 | return result; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 850 | PyDoc_STRVAR(getattr_doc, |
Guido van Rossum | 950ff29 | 1998-06-29 13:38:57 +0000 | [diff] [blame] | 851 | "getattr(object, name[, default]) -> value\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 852 | \n\ |
Guido van Rossum | 950ff29 | 1998-06-29 13:38:57 +0000 | [diff] [blame] | 853 | Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\ |
| 854 | When a default argument is given, it is returned when the attribute doesn't\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 855 | exist; without it, an exception is raised in that case."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 856 | |
| 857 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 858 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 859 | builtin_globals(PyObject *self) |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 860 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 861 | PyObject *d; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 862 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 863 | d = PyEval_GetGlobals(); |
Neal Norwitz | 43bd4db | 2006-08-12 01:46:42 +0000 | [diff] [blame] | 864 | Py_XINCREF(d); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 865 | return d; |
| 866 | } |
| 867 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 868 | PyDoc_STRVAR(globals_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 869 | "globals() -> dictionary\n\ |
| 870 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 871 | Return the dictionary containing the current scope's global variables."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 872 | |
| 873 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 874 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 875 | builtin_hasattr(PyObject *self, PyObject *args) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 876 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 877 | PyObject *v; |
| 878 | PyObject *name; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 879 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 880 | if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name)) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 881 | return NULL; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 882 | #ifdef Py_USING_UNICODE |
Jeremy Hylton | 302b54a | 2001-07-30 22:45:19 +0000 | [diff] [blame] | 883 | if (PyUnicode_Check(name)) { |
| 884 | name = _PyUnicode_AsDefaultEncodedString(name, NULL); |
| 885 | if (name == NULL) |
| 886 | return NULL; |
| 887 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 888 | #endif |
Jeremy Hylton | 302b54a | 2001-07-30 22:45:19 +0000 | [diff] [blame] | 889 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 890 | if (!PyString_Check(name)) { |
Jeremy Hylton | 302b54a | 2001-07-30 22:45:19 +0000 | [diff] [blame] | 891 | PyErr_SetString(PyExc_TypeError, |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 892 | "hasattr(): attribute name must be string"); |
Jeremy Hylton | 302b54a | 2001-07-30 22:45:19 +0000 | [diff] [blame] | 893 | return NULL; |
| 894 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 895 | v = PyObject_GetAttr(v, name); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 896 | if (v == NULL) { |
Benjamin Peterson | b9030f4 | 2008-05-12 00:41:23 +0000 | [diff] [blame] | 897 | if (!PyErr_ExceptionMatches(PyExc_Exception)) |
| 898 | return NULL; |
| 899 | else { |
| 900 | PyErr_Clear(); |
| 901 | Py_INCREF(Py_False); |
| 902 | return Py_False; |
| 903 | } |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 904 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 905 | Py_DECREF(v); |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 906 | Py_INCREF(Py_True); |
| 907 | return Py_True; |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 908 | } |
| 909 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 910 | PyDoc_STRVAR(hasattr_doc, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 911 | "hasattr(object, name) -> bool\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 912 | \n\ |
| 913 | Return whether the object has an attribute with the given name.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 914 | (This is done by calling getattr(object, name) and catching exceptions.)"); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 915 | |
| 916 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 917 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 918 | builtin_id(PyObject *self, PyObject *v) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 919 | { |
Guido van Rossum | 106f2da | 2000-06-28 21:12:25 +0000 | [diff] [blame] | 920 | return PyLong_FromVoidPtr(v); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 921 | } |
| 922 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 923 | PyDoc_STRVAR(id_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 924 | "id(object) -> integer\n\ |
| 925 | \n\ |
| 926 | Return the identity of an object. This is guaranteed to be unique among\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 927 | simultaneously existing objects. (Hint: it's the object's memory address.)"); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 928 | |
| 929 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 930 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 931 | builtin_map(PyObject *self, PyObject *args) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 932 | { |
| 933 | typedef struct { |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 934 | PyObject *it; /* the iterator object */ |
| 935 | int saw_StopIteration; /* bool: did the iterator end? */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 936 | } sequence; |
| 937 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 938 | PyObject *func, *result; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 939 | sequence *seqs = NULL, *sqp; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 940 | Py_ssize_t n, len; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 941 | register int i, j; |
| 942 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 943 | n = PyTuple_Size(args); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 944 | if (n < 2) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 945 | PyErr_SetString(PyExc_TypeError, |
| 946 | "map() requires at least two args"); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 947 | return NULL; |
| 948 | } |
| 949 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 950 | func = PyTuple_GetItem(args, 0); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 951 | n--; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 952 | |
Neal Norwitz | 53152a1 | 2008-02-24 02:20:25 +0000 | [diff] [blame] | 953 | if (func == Py_None) { |
Benjamin Peterson | 9f4f481 | 2008-04-27 03:01:45 +0000 | [diff] [blame] | 954 | if (PyErr_WarnPy3k("map(None, ...) not supported in 3.x; " |
Benjamin Peterson | f19a7b9 | 2008-04-27 18:40:21 +0000 | [diff] [blame] | 955 | "use list(...)", 1) < 0) |
Neal Norwitz | 53152a1 | 2008-02-24 02:20:25 +0000 | [diff] [blame] | 956 | return NULL; |
| 957 | if (n == 1) { |
| 958 | /* map(None, S) is the same as list(S). */ |
| 959 | return PySequence_List(PyTuple_GetItem(args, 1)); |
| 960 | } |
Guido van Rossum | fa4ac71 | 1998-07-10 17:37:30 +0000 | [diff] [blame] | 961 | } |
| 962 | |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 963 | /* Get space for sequence descriptors. Must NULL out the iterator |
| 964 | * pointers so that jumping to Fail_2 later doesn't see trash. |
| 965 | */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 966 | if ((seqs = PyMem_NEW(sequence, n)) == NULL) { |
| 967 | PyErr_NoMemory(); |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 968 | return NULL; |
| 969 | } |
| 970 | for (i = 0; i < n; ++i) { |
| 971 | seqs[i].it = (PyObject*)NULL; |
| 972 | seqs[i].saw_StopIteration = 0; |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 973 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 974 | |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 975 | /* Do a first pass to obtain iterators for the arguments, and set len |
| 976 | * to the largest of their lengths. |
Tim Peters | 748b8bb | 2001-04-28 08:20:22 +0000 | [diff] [blame] | 977 | */ |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 978 | len = 0; |
| 979 | for (i = 0, sqp = seqs; i < n; ++i, ++sqp) { |
| 980 | PyObject *curseq; |
Martin v. Löwis | d96ee90 | 2006-02-16 14:37:16 +0000 | [diff] [blame] | 981 | Py_ssize_t curlen; |
Guido van Rossum | ad99177 | 2001-01-12 16:03:05 +0000 | [diff] [blame] | 982 | |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 983 | /* Get iterator. */ |
| 984 | curseq = PyTuple_GetItem(args, i+1); |
| 985 | sqp->it = PyObject_GetIter(curseq); |
| 986 | if (sqp->it == NULL) { |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 987 | static char errmsg[] = |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 988 | "argument %d to map() must support iteration"; |
Guido van Rossum | 15e33a4 | 1997-04-30 19:00:27 +0000 | [diff] [blame] | 989 | char errbuf[sizeof(errmsg) + 25]; |
Jeremy Hylton | 518ab1c | 2001-11-28 20:42:20 +0000 | [diff] [blame] | 990 | PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 991 | PyErr_SetString(PyExc_TypeError, errbuf); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 992 | goto Fail_2; |
| 993 | } |
| 994 | |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 995 | /* Update len. */ |
Raymond Hettinger | 4e2f714 | 2007-12-06 00:56:53 +0000 | [diff] [blame] | 996 | curlen = _PyObject_LengthHint(curseq, 8); |
Raymond Hettinger | 77f3c87 | 2004-01-04 08:54:44 +0000 | [diff] [blame] | 997 | if (curlen > len) |
| 998 | len = curlen; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 1001 | /* Get space for the result list. */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1002 | if ((result = (PyObject *) PyList_New(len)) == NULL) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1003 | goto Fail_2; |
| 1004 | |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 1005 | /* Iterate over the sequences until all have stopped. */ |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1006 | for (i = 0; ; ++i) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1007 | PyObject *alist, *item=NULL, *value; |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 1008 | int numactive = 0; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1009 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1010 | if (func == Py_None && n == 1) |
Guido van Rossum | 3212031 | 1995-07-10 13:52:21 +0000 | [diff] [blame] | 1011 | alist = NULL; |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 1012 | else if ((alist = PyTuple_New(n)) == NULL) |
| 1013 | goto Fail_1; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1014 | |
| 1015 | for (j = 0, sqp = seqs; j < n; ++j, ++sqp) { |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 1016 | if (sqp->saw_StopIteration) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1017 | Py_INCREF(Py_None); |
| 1018 | item = Py_None; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1019 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1020 | else { |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 1021 | item = PyIter_Next(sqp->it); |
| 1022 | if (item) |
| 1023 | ++numactive; |
| 1024 | else { |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 1025 | if (PyErr_Occurred()) { |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 1026 | Py_XDECREF(alist); |
| 1027 | goto Fail_1; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1028 | } |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 1029 | Py_INCREF(Py_None); |
| 1030 | item = Py_None; |
| 1031 | sqp->saw_StopIteration = 1; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1032 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1033 | } |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 1034 | if (alist) |
| 1035 | PyTuple_SET_ITEM(alist, j, item); |
| 1036 | else |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1037 | break; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
Guido van Rossum | 3212031 | 1995-07-10 13:52:21 +0000 | [diff] [blame] | 1040 | if (!alist) |
| 1041 | alist = item; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1042 | |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 1043 | if (numactive == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1044 | Py_DECREF(alist); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1045 | break; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1046 | } |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1047 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1048 | if (func == Py_None) |
Guido van Rossum | 3212031 | 1995-07-10 13:52:21 +0000 | [diff] [blame] | 1049 | value = alist; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1050 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1051 | value = PyEval_CallObject(func, alist); |
| 1052 | Py_DECREF(alist); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 1053 | if (value == NULL) |
| 1054 | goto Fail_1; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1055 | } |
| 1056 | if (i >= len) { |
Barry Warsaw | fa77e09 | 1999-01-28 18:49:12 +0000 | [diff] [blame] | 1057 | int status = PyList_Append(result, value); |
Barry Warsaw | 2133287 | 1999-01-28 04:21:35 +0000 | [diff] [blame] | 1058 | Py_DECREF(value); |
Barry Warsaw | fa77e09 | 1999-01-28 18:49:12 +0000 | [diff] [blame] | 1059 | if (status < 0) |
| 1060 | goto Fail_1; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1061 | } |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 1062 | else if (PyList_SetItem(result, i, value) < 0) |
| 1063 | goto Fail_1; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Guido van Rossum | fa4ac71 | 1998-07-10 17:37:30 +0000 | [diff] [blame] | 1066 | if (i < len && PyList_SetSlice(result, i, len, NULL) < 0) |
| 1067 | goto Fail_1; |
| 1068 | |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 1069 | goto Succeed; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1070 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1071 | Fail_1: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1072 | Py_DECREF(result); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1073 | Fail_2: |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 1074 | result = NULL; |
| 1075 | Succeed: |
| 1076 | assert(seqs); |
| 1077 | for (i = 0; i < n; ++i) |
| 1078 | Py_XDECREF(seqs[i].it); |
| 1079 | PyMem_DEL(seqs); |
| 1080 | return result; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1083 | PyDoc_STRVAR(map_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1084 | "map(function, sequence[, sequence, ...]) -> list\n\ |
| 1085 | \n\ |
| 1086 | Return a list of the results of applying the function to the items of\n\ |
| 1087 | the argument sequence(s). If more than one sequence is given, the\n\ |
| 1088 | function is called with an argument list consisting of the corresponding\n\ |
| 1089 | item of each sequence, substituting None for missing values when not all\n\ |
| 1090 | sequences have the same length. If the function is None, return a list of\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1091 | the items of the sequence (or a list of tuples if more than one sequence)."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1092 | |
| 1093 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1094 | static PyObject * |
Georg Brandl | 28e0873 | 2008-04-30 19:47:09 +0000 | [diff] [blame] | 1095 | builtin_next(PyObject *self, PyObject *args) |
| 1096 | { |
| 1097 | PyObject *it, *res; |
| 1098 | PyObject *def = NULL; |
| 1099 | |
| 1100 | if (!PyArg_UnpackTuple(args, "next", 1, 2, &it, &def)) |
| 1101 | return NULL; |
| 1102 | if (!PyIter_Check(it)) { |
| 1103 | PyErr_Format(PyExc_TypeError, |
| 1104 | "%.200s object is not an iterator", |
| 1105 | it->ob_type->tp_name); |
| 1106 | return NULL; |
| 1107 | } |
| 1108 | |
| 1109 | res = (*it->ob_type->tp_iternext)(it); |
| 1110 | if (res != NULL) { |
| 1111 | return res; |
| 1112 | } else if (def != NULL) { |
| 1113 | if (PyErr_Occurred()) { |
| 1114 | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 1115 | return NULL; |
| 1116 | PyErr_Clear(); |
| 1117 | } |
| 1118 | Py_INCREF(def); |
| 1119 | return def; |
| 1120 | } else if (PyErr_Occurred()) { |
| 1121 | return NULL; |
| 1122 | } else { |
| 1123 | PyErr_SetNone(PyExc_StopIteration); |
| 1124 | return NULL; |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | PyDoc_STRVAR(next_doc, |
| 1129 | "next(iterator[, default])\n\ |
| 1130 | \n\ |
| 1131 | Return the next item from the iterator. If default is given and the iterator\n\ |
| 1132 | is exhausted, it is returned instead of raising StopIteration."); |
| 1133 | |
| 1134 | |
| 1135 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1136 | builtin_setattr(PyObject *self, PyObject *args) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 1137 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1138 | PyObject *v; |
| 1139 | PyObject *name; |
| 1140 | PyObject *value; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1141 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1142 | if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value)) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 1143 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1144 | if (PyObject_SetAttr(v, name, value) != 0) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 1145 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1146 | Py_INCREF(Py_None); |
| 1147 | return Py_None; |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1150 | PyDoc_STRVAR(setattr_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1151 | "setattr(object, name, value)\n\ |
| 1152 | \n\ |
| 1153 | Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1154 | ``x.y = v''."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1155 | |
| 1156 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1157 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1158 | builtin_delattr(PyObject *self, PyObject *args) |
Guido van Rossum | 14144fc | 1994-08-29 12:53:40 +0000 | [diff] [blame] | 1159 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1160 | PyObject *v; |
| 1161 | PyObject *name; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1162 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1163 | if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name)) |
Guido van Rossum | 14144fc | 1994-08-29 12:53:40 +0000 | [diff] [blame] | 1164 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1165 | if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0) |
Guido van Rossum | 14144fc | 1994-08-29 12:53:40 +0000 | [diff] [blame] | 1166 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1167 | Py_INCREF(Py_None); |
| 1168 | return Py_None; |
Guido van Rossum | 14144fc | 1994-08-29 12:53:40 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1171 | PyDoc_STRVAR(delattr_doc, |
Guido van Rossum | df12a59 | 1998-11-23 22:13:04 +0000 | [diff] [blame] | 1172 | "delattr(object, name)\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1173 | \n\ |
| 1174 | Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1175 | ``del x.y''."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1176 | |
| 1177 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1178 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1179 | builtin_hash(PyObject *self, PyObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1180 | { |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1181 | long x; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1182 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1183 | x = PyObject_Hash(v); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1184 | if (x == -1) |
| 1185 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1186 | return PyInt_FromLong(x); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1189 | PyDoc_STRVAR(hash_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1190 | "hash(object) -> integer\n\ |
| 1191 | \n\ |
| 1192 | Return a hash value for the object. Two objects with the same value have\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1193 | the same hash value. The reverse is not necessarily true, but likely."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1194 | |
| 1195 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1196 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1197 | builtin_hex(PyObject *self, PyObject *v) |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 1198 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1199 | PyNumberMethods *nb; |
Neil Schemenauer | 3a313e3 | 2004-07-19 16:29:17 +0000 | [diff] [blame] | 1200 | PyObject *res; |
Benjamin Peterson | c6d64ec | 2008-05-17 20:09:42 +0000 | [diff] [blame] | 1201 | |
| 1202 | if ((nb = v->ob_type->tp_as_number) == NULL || |
| 1203 | nb->nb_hex == NULL) { |
| 1204 | PyErr_SetString(PyExc_TypeError, |
| 1205 | "hex() argument can't be converted to hex"); |
| 1206 | return NULL; |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 1207 | } |
Benjamin Peterson | c6d64ec | 2008-05-17 20:09:42 +0000 | [diff] [blame] | 1208 | res = (*nb->nb_hex)(v); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1209 | if (res && !PyString_Check(res)) { |
Benjamin Peterson | c6d64ec | 2008-05-17 20:09:42 +0000 | [diff] [blame] | 1210 | PyErr_Format(PyExc_TypeError, |
| 1211 | "__hex__ returned non-string (type %.200s)", |
| 1212 | res->ob_type->tp_name); |
| 1213 | Py_DECREF(res); |
| 1214 | return NULL; |
| 1215 | } |
| 1216 | return res; |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1219 | PyDoc_STRVAR(hex_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1220 | "hex(number) -> string\n\ |
| 1221 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1222 | Return the hexadecimal representation of an integer or long integer."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1223 | |
| 1224 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 1225 | static PyObject *builtin_raw_input(PyObject *, PyObject *); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1226 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1227 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1228 | builtin_input(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1229 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1230 | PyObject *line; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1231 | char *str; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1232 | PyObject *res; |
| 1233 | PyObject *globals, *locals; |
Hye-Shik Chang | ff83c2b | 2004-02-02 13:39:01 +0000 | [diff] [blame] | 1234 | PyCompilerFlags cf; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1235 | |
| 1236 | line = builtin_raw_input(self, args); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1237 | if (line == NULL) |
| 1238 | return line; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1239 | if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1240 | return NULL; |
| 1241 | while (*str == ' ' || *str == '\t') |
| 1242 | str++; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1243 | globals = PyEval_GetGlobals(); |
| 1244 | locals = PyEval_GetLocals(); |
| 1245 | if (PyDict_GetItemString(globals, "__builtins__") == NULL) { |
| 1246 | if (PyDict_SetItemString(globals, "__builtins__", |
| 1247 | PyEval_GetBuiltins()) != 0) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 1248 | return NULL; |
| 1249 | } |
Hye-Shik Chang | ff83c2b | 2004-02-02 13:39:01 +0000 | [diff] [blame] | 1250 | cf.cf_flags = 0; |
| 1251 | PyEval_MergeCompilerFlags(&cf); |
| 1252 | res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1253 | Py_DECREF(line); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1254 | return res; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1257 | PyDoc_STRVAR(input_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1258 | "input([prompt]) -> value\n\ |
| 1259 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1260 | Equivalent to eval(raw_input(prompt))."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1261 | |
| 1262 | |
Guido van Rossum | e8811f8 | 1997-02-14 15:48:05 +0000 | [diff] [blame] | 1263 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1264 | builtin_intern(PyObject *self, PyObject *args) |
Guido van Rossum | e8811f8 | 1997-02-14 15:48:05 +0000 | [diff] [blame] | 1265 | { |
| 1266 | PyObject *s; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1267 | if (!PyArg_ParseTuple(args, "S:intern", &s)) |
Guido van Rossum | e8811f8 | 1997-02-14 15:48:05 +0000 | [diff] [blame] | 1268 | return NULL; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1269 | if (!PyString_CheckExact(s)) { |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 1270 | PyErr_SetString(PyExc_TypeError, |
| 1271 | "can't intern subclass of string"); |
| 1272 | return NULL; |
| 1273 | } |
Guido van Rossum | e8811f8 | 1997-02-14 15:48:05 +0000 | [diff] [blame] | 1274 | Py_INCREF(s); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1275 | PyString_InternInPlace(&s); |
Guido van Rossum | e8811f8 | 1997-02-14 15:48:05 +0000 | [diff] [blame] | 1276 | return s; |
| 1277 | } |
| 1278 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1279 | PyDoc_STRVAR(intern_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1280 | "intern(string) -> string\n\ |
| 1281 | \n\ |
| 1282 | ``Intern'' the given string. This enters the string in the (global)\n\ |
| 1283 | table of interned strings whose purpose is to speed up dictionary lookups.\n\ |
| 1284 | Return the string itself or the previously interned string object with the\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1285 | same value."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1286 | |
| 1287 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1288 | static PyObject * |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1289 | builtin_iter(PyObject *self, PyObject *args) |
| 1290 | { |
| 1291 | PyObject *v, *w = NULL; |
| 1292 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1293 | if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w)) |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1294 | return NULL; |
| 1295 | if (w == NULL) |
| 1296 | return PyObject_GetIter(v); |
| 1297 | if (!PyCallable_Check(v)) { |
| 1298 | PyErr_SetString(PyExc_TypeError, |
| 1299 | "iter(v, w): v must be callable"); |
| 1300 | return NULL; |
| 1301 | } |
| 1302 | return PyCallIter_New(v, w); |
| 1303 | } |
| 1304 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1305 | PyDoc_STRVAR(iter_doc, |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1306 | "iter(collection) -> iterator\n\ |
| 1307 | iter(callable, sentinel) -> iterator\n\ |
| 1308 | \n\ |
| 1309 | Get an iterator from an object. In the first form, the argument must\n\ |
| 1310 | supply its own iterator, or be a sequence.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1311 | In the second form, the callable is called until it returns the sentinel."); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1312 | |
| 1313 | |
| 1314 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1315 | builtin_len(PyObject *self, PyObject *v) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1316 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1317 | Py_ssize_t res; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1318 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1319 | res = PyObject_Size(v); |
Guido van Rossum | 8ea9f4d | 1998-06-29 22:26:50 +0000 | [diff] [blame] | 1320 | if (res < 0 && PyErr_Occurred()) |
| 1321 | return NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1322 | return PyInt_FromSsize_t(res); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1325 | PyDoc_STRVAR(len_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1326 | "len(object) -> integer\n\ |
| 1327 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1328 | Return the number of items of a sequence or mapping."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1329 | |
| 1330 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1331 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1332 | builtin_locals(PyObject *self) |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1333 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1334 | PyObject *d; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1335 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1336 | d = PyEval_GetLocals(); |
Neal Norwitz | 43bd4db | 2006-08-12 01:46:42 +0000 | [diff] [blame] | 1337 | Py_XINCREF(d); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1338 | return d; |
| 1339 | } |
| 1340 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1341 | PyDoc_STRVAR(locals_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1342 | "locals() -> dictionary\n\ |
| 1343 | \n\ |
Raymond Hettinger | 69bf8f3 | 2003-01-04 02:16:22 +0000 | [diff] [blame] | 1344 | Update and return a dictionary containing the current scope's local variables."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1345 | |
| 1346 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1347 | static PyObject * |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1348 | min_max(PyObject *args, PyObject *kwds, int op) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1349 | { |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1350 | PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL; |
Martin v. Löwis | fd39ad4 | 2004-08-12 14:42:37 +0000 | [diff] [blame] | 1351 | const char *name = op == Py_LT ? "min" : "max"; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1352 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1353 | if (PyTuple_Size(args) > 1) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1354 | v = args; |
Martin v. Löwis | fd39ad4 | 2004-08-12 14:42:37 +0000 | [diff] [blame] | 1355 | else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v)) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1356 | return NULL; |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1357 | |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1358 | if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) { |
| 1359 | keyfunc = PyDict_GetItemString(kwds, "key"); |
| 1360 | if (PyDict_Size(kwds)!=1 || keyfunc == NULL) { |
Georg Brandl | 99d7e4e | 2005-08-31 22:21:15 +0000 | [diff] [blame] | 1361 | PyErr_Format(PyExc_TypeError, |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1362 | "%s() got an unexpected keyword argument", name); |
| 1363 | return NULL; |
| 1364 | } |
Guido van Rossum | 1d9a9ea | 2008-01-23 20:19:01 +0000 | [diff] [blame] | 1365 | Py_INCREF(keyfunc); |
Georg Brandl | 99d7e4e | 2005-08-31 22:21:15 +0000 | [diff] [blame] | 1366 | } |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1367 | |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 1368 | it = PyObject_GetIter(v); |
Guido van Rossum | 1d9a9ea | 2008-01-23 20:19:01 +0000 | [diff] [blame] | 1369 | if (it == NULL) { |
| 1370 | Py_XDECREF(keyfunc); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1371 | return NULL; |
Guido van Rossum | 1d9a9ea | 2008-01-23 20:19:01 +0000 | [diff] [blame] | 1372 | } |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 1373 | |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1374 | maxitem = NULL; /* the result */ |
| 1375 | maxval = NULL; /* the value associated with the result */ |
Brett Cannon | 9e635cf | 2004-12-07 00:25:35 +0000 | [diff] [blame] | 1376 | while (( item = PyIter_Next(it) )) { |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1377 | /* get the value from the key function */ |
| 1378 | if (keyfunc != NULL) { |
| 1379 | val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL); |
| 1380 | if (val == NULL) |
| 1381 | goto Fail_it_item; |
| 1382 | } |
| 1383 | /* no key function; the value is the item */ |
| 1384 | else { |
| 1385 | val = item; |
| 1386 | Py_INCREF(val); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1387 | } |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 1388 | |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1389 | /* maximum value and item are unset; set them */ |
| 1390 | if (maxval == NULL) { |
| 1391 | maxitem = item; |
| 1392 | maxval = val; |
| 1393 | } |
| 1394 | /* maximum value and item are set; update them as necessary */ |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1395 | else { |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1396 | int cmp = PyObject_RichCompareBool(val, maxval, op); |
| 1397 | if (cmp < 0) |
| 1398 | goto Fail_it_item_and_val; |
| 1399 | else if (cmp > 0) { |
| 1400 | Py_DECREF(maxval); |
| 1401 | Py_DECREF(maxitem); |
| 1402 | maxval = val; |
| 1403 | maxitem = item; |
Guido van Rossum | 53451b3 | 2001-01-17 15:47:24 +0000 | [diff] [blame] | 1404 | } |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1405 | else { |
| 1406 | Py_DECREF(item); |
| 1407 | Py_DECREF(val); |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 1408 | } |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1409 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1410 | } |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1411 | if (PyErr_Occurred()) |
| 1412 | goto Fail_it; |
| 1413 | if (maxval == NULL) { |
Martin v. Löwis | fd39ad4 | 2004-08-12 14:42:37 +0000 | [diff] [blame] | 1414 | PyErr_Format(PyExc_ValueError, |
| 1415 | "%s() arg is an empty sequence", name); |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1416 | assert(maxitem == NULL); |
| 1417 | } |
| 1418 | else |
| 1419 | Py_DECREF(maxval); |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 1420 | Py_DECREF(it); |
Guido van Rossum | 1d9a9ea | 2008-01-23 20:19:01 +0000 | [diff] [blame] | 1421 | Py_XDECREF(keyfunc); |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1422 | return maxitem; |
| 1423 | |
| 1424 | Fail_it_item_and_val: |
| 1425 | Py_DECREF(val); |
| 1426 | Fail_it_item: |
| 1427 | Py_DECREF(item); |
| 1428 | Fail_it: |
| 1429 | Py_XDECREF(maxval); |
| 1430 | Py_XDECREF(maxitem); |
| 1431 | Py_DECREF(it); |
Guido van Rossum | 1d9a9ea | 2008-01-23 20:19:01 +0000 | [diff] [blame] | 1432 | Py_XDECREF(keyfunc); |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1433 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1436 | static PyObject * |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1437 | builtin_min(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1438 | { |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1439 | return min_max(args, kwds, Py_LT); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1442 | PyDoc_STRVAR(min_doc, |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1443 | "min(iterable[, key=func]) -> value\n\ |
| 1444 | min(a, b, c, ...[, key=func]) -> value\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1445 | \n\ |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1446 | With a single iterable argument, return its smallest item.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1447 | With two or more arguments, return the smallest argument."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1448 | |
| 1449 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1450 | static PyObject * |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1451 | builtin_max(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1452 | { |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1453 | return min_max(args, kwds, Py_GT); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1456 | PyDoc_STRVAR(max_doc, |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1457 | "max(iterable[, key=func]) -> value\n\ |
| 1458 | max(a, b, c, ...[, key=func]) -> value\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1459 | \n\ |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1460 | With a single iterable argument, return its largest item.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1461 | With two or more arguments, return the largest argument."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1462 | |
| 1463 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1464 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1465 | builtin_oct(PyObject *self, PyObject *v) |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 1466 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1467 | PyNumberMethods *nb; |
Neil Schemenauer | 3a313e3 | 2004-07-19 16:29:17 +0000 | [diff] [blame] | 1468 | PyObject *res; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1469 | |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1470 | if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL || |
| 1471 | nb->nb_oct == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1472 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1473 | "oct() argument can't be converted to oct"); |
| 1474 | return NULL; |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 1475 | } |
Neil Schemenauer | 3a313e3 | 2004-07-19 16:29:17 +0000 | [diff] [blame] | 1476 | res = (*nb->nb_oct)(v); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1477 | if (res && !PyString_Check(res)) { |
Neil Schemenauer | 3a313e3 | 2004-07-19 16:29:17 +0000 | [diff] [blame] | 1478 | PyErr_Format(PyExc_TypeError, |
| 1479 | "__oct__ returned non-string (type %.200s)", |
| 1480 | res->ob_type->tp_name); |
| 1481 | Py_DECREF(res); |
| 1482 | return NULL; |
| 1483 | } |
| 1484 | return res; |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1487 | PyDoc_STRVAR(oct_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1488 | "oct(number) -> string\n\ |
| 1489 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1490 | Return the octal representation of an integer or long integer."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1491 | |
| 1492 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1493 | static PyObject * |
Neal Norwitz | c4edb0e | 2006-05-02 04:43:14 +0000 | [diff] [blame] | 1494 | builtin_open(PyObject *self, PyObject *args, PyObject *kwds) |
| 1495 | { |
| 1496 | return PyObject_Call((PyObject*)&PyFile_Type, args, kwds); |
| 1497 | } |
| 1498 | |
| 1499 | PyDoc_STRVAR(open_doc, |
| 1500 | "open(name[, mode[, buffering]]) -> file object\n\ |
| 1501 | \n\ |
Skip Montanaro | 4e3ebe0 | 2007-12-08 14:37:43 +0000 | [diff] [blame] | 1502 | Open a file using the file() type, returns a file object. This is the\n\ |
| 1503 | preferred way to open a file."); |
Neal Norwitz | c4edb0e | 2006-05-02 04:43:14 +0000 | [diff] [blame] | 1504 | |
| 1505 | |
| 1506 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1507 | builtin_ord(PyObject *self, PyObject* obj) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1508 | { |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 1509 | long ord; |
Martin v. Löwis | d96ee90 | 2006-02-16 14:37:16 +0000 | [diff] [blame] | 1510 | Py_ssize_t size; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1511 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1512 | if (PyString_Check(obj)) { |
| 1513 | size = PyString_GET_SIZE(obj); |
Andrew M. Kuchling | 34c20cf | 2000-12-20 14:36:56 +0000 | [diff] [blame] | 1514 | if (size == 1) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1515 | ord = (long)((unsigned char)*PyString_AS_STRING(obj)); |
Andrew M. Kuchling | 34c20cf | 2000-12-20 14:36:56 +0000 | [diff] [blame] | 1516 | return PyInt_FromLong(ord); |
| 1517 | } |
Christian Heimes | 3497f94 | 2008-05-26 12:29:14 +0000 | [diff] [blame] | 1518 | } else if (PyByteArray_Check(obj)) { |
| 1519 | size = PyByteArray_GET_SIZE(obj); |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 1520 | if (size == 1) { |
Christian Heimes | 3497f94 | 2008-05-26 12:29:14 +0000 | [diff] [blame] | 1521 | ord = (long)((unsigned char)*PyByteArray_AS_STRING(obj)); |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 1522 | return PyInt_FromLong(ord); |
| 1523 | } |
| 1524 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1525 | #ifdef Py_USING_UNICODE |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1526 | } else if (PyUnicode_Check(obj)) { |
| 1527 | size = PyUnicode_GET_SIZE(obj); |
Andrew M. Kuchling | 34c20cf | 2000-12-20 14:36:56 +0000 | [diff] [blame] | 1528 | if (size == 1) { |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1529 | ord = (long)*PyUnicode_AS_UNICODE(obj); |
Andrew M. Kuchling | 34c20cf | 2000-12-20 14:36:56 +0000 | [diff] [blame] | 1530 | return PyInt_FromLong(ord); |
| 1531 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1532 | #endif |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1533 | } else { |
| 1534 | PyErr_Format(PyExc_TypeError, |
Andrew M. Kuchling | f07aad1 | 2000-12-23 14:11:28 +0000 | [diff] [blame] | 1535 | "ord() expected string of length 1, but " \ |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1536 | "%.200s found", obj->ob_type->tp_name); |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 1537 | return NULL; |
| 1538 | } |
| 1539 | |
Guido van Rossum | ad99177 | 2001-01-12 16:03:05 +0000 | [diff] [blame] | 1540 | PyErr_Format(PyExc_TypeError, |
Andrew M. Kuchling | f07aad1 | 2000-12-23 14:11:28 +0000 | [diff] [blame] | 1541 | "ord() expected a character, " |
Martin v. Löwis | d96ee90 | 2006-02-16 14:37:16 +0000 | [diff] [blame] | 1542 | "but string of length %zd found", |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1543 | size); |
| 1544 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1547 | PyDoc_STRVAR(ord_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1548 | "ord(c) -> integer\n\ |
| 1549 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1550 | Return the integer ordinal of a one-character string."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1551 | |
| 1552 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1553 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1554 | builtin_pow(PyObject *self, PyObject *args) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1555 | { |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 1556 | PyObject *v, *w, *z = Py_None; |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1557 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1558 | if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z)) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1559 | return NULL; |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 1560 | return PyNumber_Power(v, w, z); |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1563 | PyDoc_STRVAR(pow_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1564 | "pow(x, y[, z]) -> number\n\ |
| 1565 | \n\ |
| 1566 | With two arguments, equivalent to x**y. With three arguments,\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1567 | equivalent to (x**y) % z, but may be more efficient (e.g. for longs)."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1568 | |
| 1569 | |
Eric Smith | 7c47894 | 2008-03-18 23:45:49 +0000 | [diff] [blame] | 1570 | static PyObject * |
| 1571 | builtin_print(PyObject *self, PyObject *args, PyObject *kwds) |
| 1572 | { |
| 1573 | static char *kwlist[] = {"sep", "end", "file", 0}; |
| 1574 | static PyObject *dummy_args; |
| 1575 | PyObject *sep = NULL, *end = NULL, *file = NULL; |
| 1576 | int i, err; |
| 1577 | |
| 1578 | if (dummy_args == NULL) { |
| 1579 | if (!(dummy_args = PyTuple_New(0))) |
| 1580 | return NULL; |
| 1581 | } |
| 1582 | if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print", |
| 1583 | kwlist, &sep, &end, &file)) |
| 1584 | return NULL; |
| 1585 | if (file == NULL || file == Py_None) { |
| 1586 | file = PySys_GetObject("stdout"); |
| 1587 | /* sys.stdout may be None when FILE* stdout isn't connected */ |
| 1588 | if (file == Py_None) |
| 1589 | Py_RETURN_NONE; |
| 1590 | } |
| 1591 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1592 | if (sep && sep != Py_None && !PyString_Check(sep) && |
Eric Smith | 7c47894 | 2008-03-18 23:45:49 +0000 | [diff] [blame] | 1593 | !PyUnicode_Check(sep)) { |
| 1594 | PyErr_Format(PyExc_TypeError, |
| 1595 | "sep must be None, str or unicode, not %.200s", |
| 1596 | sep->ob_type->tp_name); |
| 1597 | return NULL; |
| 1598 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1599 | if (end && end != Py_None && !PyString_Check(end) && |
Eric Smith | 7c47894 | 2008-03-18 23:45:49 +0000 | [diff] [blame] | 1600 | !PyUnicode_Check(end)) { |
| 1601 | PyErr_Format(PyExc_TypeError, |
| 1602 | "end must be None, str or unicode, not %.200s", |
| 1603 | end->ob_type->tp_name); |
| 1604 | return NULL; |
| 1605 | } |
| 1606 | |
| 1607 | for (i = 0; i < PyTuple_Size(args); i++) { |
| 1608 | if (i > 0) { |
| 1609 | if (sep == NULL || sep == Py_None) |
| 1610 | err = PyFile_WriteString(" ", file); |
| 1611 | else |
| 1612 | err = PyFile_WriteObject(sep, file, |
| 1613 | Py_PRINT_RAW); |
| 1614 | if (err) |
| 1615 | return NULL; |
| 1616 | } |
| 1617 | err = PyFile_WriteObject(PyTuple_GetItem(args, i), file, |
| 1618 | Py_PRINT_RAW); |
| 1619 | if (err) |
| 1620 | return NULL; |
| 1621 | } |
| 1622 | |
| 1623 | if (end == NULL || end == Py_None) |
| 1624 | err = PyFile_WriteString("\n", file); |
| 1625 | else |
| 1626 | err = PyFile_WriteObject(end, file, Py_PRINT_RAW); |
| 1627 | if (err) |
| 1628 | return NULL; |
| 1629 | |
| 1630 | Py_RETURN_NONE; |
| 1631 | } |
| 1632 | |
| 1633 | PyDoc_STRVAR(print_doc, |
| 1634 | "print(value, ..., sep=' ', end='\\n', file=sys.stdout)\n\ |
| 1635 | \n\ |
| 1636 | Prints the values to a stream, or to sys.stdout by default.\n\ |
| 1637 | Optional keyword arguments:\n\ |
| 1638 | file: a file-like object (stream); defaults to the current sys.stdout.\n\ |
| 1639 | sep: string inserted between values, default a space.\n\ |
| 1640 | end: string appended after the last value, default a newline."); |
| 1641 | |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1642 | |
| 1643 | /* Return number of items in range (lo, hi, step), when arguments are |
| 1644 | * PyInt or PyLong objects. step > 0 required. Return a value < 0 if |
| 1645 | * & only if the true value is too large to fit in a signed long. |
| 1646 | * Arguments MUST return 1 with either PyInt_Check() or |
| 1647 | * PyLong_Check(). Return -1 when there is an error. |
| 1648 | */ |
| 1649 | static long |
| 1650 | get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step) |
| 1651 | { |
| 1652 | /* ------------------------------------------------------------- |
| 1653 | Algorithm is equal to that of get_len_of_range(), but it operates |
| 1654 | on PyObjects (which are assumed to be PyLong or PyInt objects). |
| 1655 | ---------------------------------------------------------------*/ |
| 1656 | long n; |
| 1657 | PyObject *diff = NULL; |
| 1658 | PyObject *one = NULL; |
| 1659 | PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL; |
| 1660 | /* holds sub-expression evaluations */ |
| 1661 | |
| 1662 | /* if (lo >= hi), return length of 0. */ |
| 1663 | if (PyObject_Compare(lo, hi) >= 0) |
| 1664 | return 0; |
| 1665 | |
| 1666 | if ((one = PyLong_FromLong(1L)) == NULL) |
| 1667 | goto Fail; |
| 1668 | |
| 1669 | if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL) |
| 1670 | goto Fail; |
| 1671 | |
| 1672 | if ((diff = PyNumber_Subtract(tmp1, one)) == NULL) |
| 1673 | goto Fail; |
| 1674 | |
| 1675 | if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL) |
| 1676 | goto Fail; |
| 1677 | |
| 1678 | if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL) |
| 1679 | goto Fail; |
| 1680 | |
| 1681 | n = PyLong_AsLong(tmp3); |
| 1682 | if (PyErr_Occurred()) { /* Check for Overflow */ |
| 1683 | PyErr_Clear(); |
| 1684 | goto Fail; |
| 1685 | } |
| 1686 | |
| 1687 | Py_DECREF(tmp3); |
| 1688 | Py_DECREF(tmp2); |
| 1689 | Py_DECREF(diff); |
| 1690 | Py_DECREF(tmp1); |
| 1691 | Py_DECREF(one); |
| 1692 | return n; |
| 1693 | |
| 1694 | Fail: |
| 1695 | Py_XDECREF(tmp3); |
| 1696 | Py_XDECREF(tmp2); |
| 1697 | Py_XDECREF(diff); |
| 1698 | Py_XDECREF(tmp1); |
| 1699 | Py_XDECREF(one); |
| 1700 | return -1; |
| 1701 | } |
| 1702 | |
| 1703 | /* An extension of builtin_range() that handles the case when PyLong |
| 1704 | * arguments are given. */ |
| 1705 | static PyObject * |
| 1706 | handle_range_longs(PyObject *self, PyObject *args) |
| 1707 | { |
| 1708 | PyObject *ilow; |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1709 | PyObject *ihigh = NULL; |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1710 | PyObject *istep = NULL; |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1711 | |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1712 | PyObject *curnum = NULL; |
| 1713 | PyObject *v = NULL; |
| 1714 | long bign; |
| 1715 | int i, n; |
| 1716 | int cmp_result; |
| 1717 | |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1718 | PyObject *zero = PyLong_FromLong(0); |
| 1719 | |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1720 | if (zero == NULL) |
| 1721 | return NULL; |
| 1722 | |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1723 | if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) { |
| 1724 | Py_DECREF(zero); |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1725 | return NULL; |
| 1726 | } |
| 1727 | |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1728 | /* Figure out which way we were called, supply defaults, and be |
| 1729 | * sure to incref everything so that the decrefs at the end |
| 1730 | * are correct. |
| 1731 | */ |
| 1732 | assert(ilow != NULL); |
| 1733 | if (ihigh == NULL) { |
| 1734 | /* only 1 arg -- it's the upper limit */ |
| 1735 | ihigh = ilow; |
| 1736 | ilow = NULL; |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1737 | } |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1738 | assert(ihigh != NULL); |
| 1739 | Py_INCREF(ihigh); |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1740 | |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1741 | /* ihigh correct now; do ilow */ |
| 1742 | if (ilow == NULL) |
| 1743 | ilow = zero; |
| 1744 | Py_INCREF(ilow); |
| 1745 | |
| 1746 | /* ilow and ihigh correct now; do istep */ |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1747 | if (istep == NULL) { |
| 1748 | istep = PyLong_FromLong(1L); |
| 1749 | if (istep == NULL) |
| 1750 | goto Fail; |
| 1751 | } |
| 1752 | else { |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1753 | Py_INCREF(istep); |
| 1754 | } |
| 1755 | |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1756 | if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) { |
Guido van Rossum | 28e83e3 | 2003-04-15 12:43:26 +0000 | [diff] [blame] | 1757 | PyErr_Format(PyExc_TypeError, |
Alex Martelli | f471d47 | 2003-04-23 13:00:44 +0000 | [diff] [blame] | 1758 | "range() integer start argument expected, got %s.", |
Guido van Rossum | 817d6c9 | 2003-04-14 18:25:04 +0000 | [diff] [blame] | 1759 | ilow->ob_type->tp_name); |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1760 | goto Fail; |
| 1761 | } |
| 1762 | |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1763 | if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) { |
Guido van Rossum | 28e83e3 | 2003-04-15 12:43:26 +0000 | [diff] [blame] | 1764 | PyErr_Format(PyExc_TypeError, |
Alex Martelli | f471d47 | 2003-04-23 13:00:44 +0000 | [diff] [blame] | 1765 | "range() integer end argument expected, got %s.", |
Guido van Rossum | 817d6c9 | 2003-04-14 18:25:04 +0000 | [diff] [blame] | 1766 | ihigh->ob_type->tp_name); |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1767 | goto Fail; |
| 1768 | } |
| 1769 | |
| 1770 | if (!PyInt_Check(istep) && !PyLong_Check(istep)) { |
Guido van Rossum | 28e83e3 | 2003-04-15 12:43:26 +0000 | [diff] [blame] | 1771 | PyErr_Format(PyExc_TypeError, |
Alex Martelli | f471d47 | 2003-04-23 13:00:44 +0000 | [diff] [blame] | 1772 | "range() integer step argument expected, got %s.", |
Guido van Rossum | 817d6c9 | 2003-04-14 18:25:04 +0000 | [diff] [blame] | 1773 | istep->ob_type->tp_name); |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1774 | goto Fail; |
| 1775 | } |
| 1776 | |
| 1777 | if (PyObject_Cmp(istep, zero, &cmp_result) == -1) |
| 1778 | goto Fail; |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1779 | if (cmp_result == 0) { |
| 1780 | PyErr_SetString(PyExc_ValueError, |
Alex Martelli | f471d47 | 2003-04-23 13:00:44 +0000 | [diff] [blame] | 1781 | "range() step argument must not be zero"); |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1782 | goto Fail; |
| 1783 | } |
| 1784 | |
| 1785 | if (cmp_result > 0) |
| 1786 | bign = get_len_of_range_longs(ilow, ihigh, istep); |
| 1787 | else { |
| 1788 | PyObject *neg_istep = PyNumber_Negative(istep); |
| 1789 | if (neg_istep == NULL) |
| 1790 | goto Fail; |
| 1791 | bign = get_len_of_range_longs(ihigh, ilow, neg_istep); |
| 1792 | Py_DECREF(neg_istep); |
| 1793 | } |
| 1794 | |
| 1795 | n = (int)bign; |
| 1796 | if (bign < 0 || (long)n != bign) { |
| 1797 | PyErr_SetString(PyExc_OverflowError, |
| 1798 | "range() result has too many items"); |
| 1799 | goto Fail; |
| 1800 | } |
| 1801 | |
| 1802 | v = PyList_New(n); |
| 1803 | if (v == NULL) |
| 1804 | goto Fail; |
| 1805 | |
| 1806 | curnum = ilow; |
| 1807 | Py_INCREF(curnum); |
| 1808 | |
| 1809 | for (i = 0; i < n; i++) { |
| 1810 | PyObject *w = PyNumber_Long(curnum); |
| 1811 | PyObject *tmp_num; |
| 1812 | if (w == NULL) |
| 1813 | goto Fail; |
| 1814 | |
| 1815 | PyList_SET_ITEM(v, i, w); |
| 1816 | |
| 1817 | tmp_num = PyNumber_Add(curnum, istep); |
| 1818 | if (tmp_num == NULL) |
| 1819 | goto Fail; |
| 1820 | |
| 1821 | Py_DECREF(curnum); |
| 1822 | curnum = tmp_num; |
| 1823 | } |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1824 | Py_DECREF(ilow); |
| 1825 | Py_DECREF(ihigh); |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1826 | Py_DECREF(istep); |
| 1827 | Py_DECREF(zero); |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1828 | Py_DECREF(curnum); |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1829 | return v; |
| 1830 | |
| 1831 | Fail: |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1832 | Py_DECREF(ilow); |
| 1833 | Py_DECREF(ihigh); |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1834 | Py_XDECREF(istep); |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1835 | Py_DECREF(zero); |
| 1836 | Py_XDECREF(curnum); |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1837 | Py_XDECREF(v); |
| 1838 | return NULL; |
| 1839 | } |
| 1840 | |
Guido van Rossum | 124eff0 | 1999-02-23 16:11:01 +0000 | [diff] [blame] | 1841 | /* Return number of items in range/xrange (lo, hi, step). step > 0 |
| 1842 | * required. Return a value < 0 if & only if the true value is too |
| 1843 | * large to fit in a signed long. |
| 1844 | */ |
| 1845 | static long |
Thomas Wouters | e28c296 | 2000-07-23 22:21:32 +0000 | [diff] [blame] | 1846 | get_len_of_range(long lo, long hi, long step) |
Guido van Rossum | 124eff0 | 1999-02-23 16:11:01 +0000 | [diff] [blame] | 1847 | { |
| 1848 | /* ------------------------------------------------------------- |
| 1849 | If lo >= hi, the range is empty. |
| 1850 | Else if n values are in the range, the last one is |
| 1851 | lo + (n-1)*step, which must be <= hi-1. Rearranging, |
| 1852 | n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives |
| 1853 | the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so |
| 1854 | the RHS is non-negative and so truncation is the same as the |
| 1855 | floor. Letting M be the largest positive long, the worst case |
| 1856 | for the RHS numerator is hi=M, lo=-M-1, and then |
| 1857 | hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough |
| 1858 | precision to compute the RHS exactly. |
| 1859 | ---------------------------------------------------------------*/ |
| 1860 | long n = 0; |
| 1861 | if (lo < hi) { |
| 1862 | unsigned long uhi = (unsigned long)hi; |
| 1863 | unsigned long ulo = (unsigned long)lo; |
| 1864 | unsigned long diff = uhi - ulo - 1; |
| 1865 | n = (long)(diff / (unsigned long)step + 1); |
| 1866 | } |
| 1867 | return n; |
| 1868 | } |
| 1869 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1870 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1871 | builtin_range(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1872 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1873 | long ilow = 0, ihigh = 0, istep = 1; |
Guido van Rossum | 124eff0 | 1999-02-23 16:11:01 +0000 | [diff] [blame] | 1874 | long bign; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1875 | int i, n; |
Guido van Rossum | 124eff0 | 1999-02-23 16:11:01 +0000 | [diff] [blame] | 1876 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1877 | PyObject *v; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1878 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1879 | if (PyTuple_Size(args) <= 1) { |
| 1880 | if (!PyArg_ParseTuple(args, |
Guido van Rossum | 0865dd9 | 1995-01-17 16:30:22 +0000 | [diff] [blame] | 1881 | "l;range() requires 1-3 int arguments", |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1882 | &ihigh)) { |
| 1883 | PyErr_Clear(); |
| 1884 | return handle_range_longs(self, args); |
| 1885 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1886 | } |
| 1887 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1888 | if (!PyArg_ParseTuple(args, |
Guido van Rossum | 0865dd9 | 1995-01-17 16:30:22 +0000 | [diff] [blame] | 1889 | "ll|l;range() requires 1-3 int arguments", |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1890 | &ilow, &ihigh, &istep)) { |
| 1891 | PyErr_Clear(); |
| 1892 | return handle_range_longs(self, args); |
| 1893 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1894 | } |
| 1895 | if (istep == 0) { |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1896 | PyErr_SetString(PyExc_ValueError, |
Alex Martelli | f471d47 | 2003-04-23 13:00:44 +0000 | [diff] [blame] | 1897 | "range() step argument must not be zero"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1898 | return NULL; |
| 1899 | } |
Guido van Rossum | 124eff0 | 1999-02-23 16:11:01 +0000 | [diff] [blame] | 1900 | if (istep > 0) |
| 1901 | bign = get_len_of_range(ilow, ihigh, istep); |
| 1902 | else |
| 1903 | bign = get_len_of_range(ihigh, ilow, -istep); |
| 1904 | n = (int)bign; |
| 1905 | if (bign < 0 || (long)n != bign) { |
Guido van Rossum | e23cde2 | 1999-01-12 05:07:47 +0000 | [diff] [blame] | 1906 | PyErr_SetString(PyExc_OverflowError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1907 | "range() result has too many items"); |
Guido van Rossum | e23cde2 | 1999-01-12 05:07:47 +0000 | [diff] [blame] | 1908 | return NULL; |
| 1909 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1910 | v = PyList_New(n); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1911 | if (v == NULL) |
| 1912 | return NULL; |
| 1913 | for (i = 0; i < n; i++) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1914 | PyObject *w = PyInt_FromLong(ilow); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1915 | if (w == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1916 | Py_DECREF(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1917 | return NULL; |
| 1918 | } |
Guido van Rossum | a937d14 | 1998-04-24 18:22:02 +0000 | [diff] [blame] | 1919 | PyList_SET_ITEM(v, i, w); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1920 | ilow += istep; |
| 1921 | } |
| 1922 | return v; |
| 1923 | } |
| 1924 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1925 | PyDoc_STRVAR(range_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1926 | "range([start,] stop[, step]) -> list of integers\n\ |
| 1927 | \n\ |
| 1928 | Return a list containing an arithmetic progression of integers.\n\ |
| 1929 | range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\ |
| 1930 | When step is given, it specifies the increment (or decrement).\n\ |
| 1931 | For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1932 | These are exactly the valid indices for a list of 4 elements."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1933 | |
| 1934 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1935 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1936 | builtin_raw_input(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1937 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1938 | PyObject *v = NULL; |
Martin v. Löwis | 31d2df5 | 2002-08-14 15:46:02 +0000 | [diff] [blame] | 1939 | PyObject *fin = PySys_GetObject("stdin"); |
| 1940 | PyObject *fout = PySys_GetObject("stdout"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1941 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1942 | if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v)) |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1943 | return NULL; |
Martin v. Löwis | 31d2df5 | 2002-08-14 15:46:02 +0000 | [diff] [blame] | 1944 | |
| 1945 | if (fin == NULL) { |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 1946 | PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin"); |
Martin v. Löwis | 31d2df5 | 2002-08-14 15:46:02 +0000 | [diff] [blame] | 1947 | return NULL; |
| 1948 | } |
| 1949 | if (fout == NULL) { |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 1950 | PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout"); |
Martin v. Löwis | 31d2df5 | 2002-08-14 15:46:02 +0000 | [diff] [blame] | 1951 | return NULL; |
| 1952 | } |
| 1953 | if (PyFile_SoftSpace(fout, 0)) { |
| 1954 | if (PyFile_WriteString(" ", fout) != 0) |
| 1955 | return NULL; |
| 1956 | } |
Georg Brandl | 7e3ba2a | 2006-08-06 08:23:54 +0000 | [diff] [blame] | 1957 | if (PyFile_AsFile(fin) && PyFile_AsFile(fout) |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 1958 | && isatty(fileno(PyFile_AsFile(fin))) |
| 1959 | && isatty(fileno(PyFile_AsFile(fout)))) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1960 | PyObject *po; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1961 | char *prompt; |
| 1962 | char *s; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1963 | PyObject *result; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1964 | if (v != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1965 | po = PyObject_Str(v); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1966 | if (po == NULL) |
| 1967 | return NULL; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1968 | prompt = PyString_AsString(po); |
Guido van Rossum | d9b5208 | 1998-06-26 18:25:38 +0000 | [diff] [blame] | 1969 | if (prompt == NULL) |
| 1970 | return NULL; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1971 | } |
| 1972 | else { |
| 1973 | po = NULL; |
| 1974 | prompt = ""; |
| 1975 | } |
Michael W. Hudson | 52db519 | 2004-08-02 13:21:09 +0000 | [diff] [blame] | 1976 | s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout), |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 1977 | prompt); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1978 | Py_XDECREF(po); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1979 | if (s == NULL) { |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 1980 | if (!PyErr_Occurred()) |
| 1981 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1982 | return NULL; |
| 1983 | } |
| 1984 | if (*s == '\0') { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1985 | PyErr_SetNone(PyExc_EOFError); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1986 | result = NULL; |
| 1987 | } |
| 1988 | else { /* strip trailing '\n' */ |
Guido van Rossum | 106f2da | 2000-06-28 21:12:25 +0000 | [diff] [blame] | 1989 | size_t len = strlen(s); |
Martin v. Löwis | b1ed7fa | 2006-04-13 07:52:27 +0000 | [diff] [blame] | 1990 | if (len > PY_SSIZE_T_MAX) { |
Martin v. Löwis | 31d2df5 | 2002-08-14 15:46:02 +0000 | [diff] [blame] | 1991 | PyErr_SetString(PyExc_OverflowError, |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 1992 | "[raw_]input: input too long"); |
Guido van Rossum | 106f2da | 2000-06-28 21:12:25 +0000 | [diff] [blame] | 1993 | result = NULL; |
| 1994 | } |
| 1995 | else { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1996 | result = PyString_FromStringAndSize(s, len-1); |
Guido van Rossum | 106f2da | 2000-06-28 21:12:25 +0000 | [diff] [blame] | 1997 | } |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1998 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1999 | PyMem_FREE(s); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 2000 | return result; |
| 2001 | } |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 2002 | if (v != NULL) { |
Martin v. Löwis | 31d2df5 | 2002-08-14 15:46:02 +0000 | [diff] [blame] | 2003 | if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0) |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 2004 | return NULL; |
| 2005 | } |
Martin v. Löwis | 31d2df5 | 2002-08-14 15:46:02 +0000 | [diff] [blame] | 2006 | return PyFile_GetLine(fin, -1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2009 | PyDoc_STRVAR(raw_input_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2010 | "raw_input([prompt]) -> string\n\ |
| 2011 | \n\ |
| 2012 | Read a string from standard input. The trailing newline is stripped.\n\ |
| 2013 | If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\ |
| 2014 | On Unix, GNU readline is used if enabled. The prompt string, if given,\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2015 | is printed without a trailing newline before reading."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2016 | |
| 2017 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2018 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2019 | builtin_reduce(PyObject *self, PyObject *args) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2020 | { |
Tim Peters | 15d81ef | 2001-05-04 04:39:21 +0000 | [diff] [blame] | 2021 | PyObject *seq, *func, *result = NULL, *it; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2022 | |
Benjamin Peterson | 9f4f481 | 2008-04-27 03:01:45 +0000 | [diff] [blame] | 2023 | if (PyErr_WarnPy3k("reduce() not supported in 3.x; " |
Benjamin Peterson | f19a7b9 | 2008-04-27 18:40:21 +0000 | [diff] [blame] | 2024 | "use functools.reduce()", 1) < 0) |
Neal Norwitz | df25efe | 2007-05-23 06:58:36 +0000 | [diff] [blame] | 2025 | return NULL; |
| 2026 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 2027 | if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2028 | return NULL; |
| 2029 | if (result != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2030 | Py_INCREF(result); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2031 | |
Tim Peters | 15d81ef | 2001-05-04 04:39:21 +0000 | [diff] [blame] | 2032 | it = PyObject_GetIter(seq); |
| 2033 | if (it == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2034 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | 15d81ef | 2001-05-04 04:39:21 +0000 | [diff] [blame] | 2035 | "reduce() arg 2 must support iteration"); |
| 2036 | Py_XDECREF(result); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2037 | return NULL; |
| 2038 | } |
| 2039 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2040 | if ((args = PyTuple_New(2)) == NULL) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 2041 | goto Fail; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2042 | |
Tim Peters | 15d81ef | 2001-05-04 04:39:21 +0000 | [diff] [blame] | 2043 | for (;;) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2044 | PyObject *op2; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2045 | |
| 2046 | if (args->ob_refcnt > 1) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2047 | Py_DECREF(args); |
| 2048 | if ((args = PyTuple_New(2)) == NULL) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 2049 | goto Fail; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
Tim Peters | 15d81ef | 2001-05-04 04:39:21 +0000 | [diff] [blame] | 2052 | op2 = PyIter_Next(it); |
| 2053 | if (op2 == NULL) { |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 2054 | if (PyErr_Occurred()) |
| 2055 | goto Fail; |
| 2056 | break; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 2057 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2058 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 2059 | if (result == NULL) |
| 2060 | result = op2; |
| 2061 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2062 | PyTuple_SetItem(args, 0, result); |
| 2063 | PyTuple_SetItem(args, 1, op2); |
| 2064 | if ((result = PyEval_CallObject(func, args)) == NULL) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 2065 | goto Fail; |
| 2066 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2069 | Py_DECREF(args); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2070 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 2071 | if (result == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2072 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 2073 | "reduce() of empty sequence with no initial value"); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 2074 | |
Tim Peters | 15d81ef | 2001-05-04 04:39:21 +0000 | [diff] [blame] | 2075 | Py_DECREF(it); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2076 | return result; |
| 2077 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 2078 | Fail: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2079 | Py_XDECREF(args); |
| 2080 | Py_XDECREF(result); |
Tim Peters | 15d81ef | 2001-05-04 04:39:21 +0000 | [diff] [blame] | 2081 | Py_DECREF(it); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2082 | return NULL; |
| 2083 | } |
| 2084 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2085 | PyDoc_STRVAR(reduce_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2086 | "reduce(function, sequence[, initial]) -> value\n\ |
| 2087 | \n\ |
| 2088 | Apply a function of two arguments cumulatively to the items of a sequence,\n\ |
| 2089 | from left to right, so as to reduce the sequence to a single value.\n\ |
| 2090 | For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\ |
| 2091 | ((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\ |
| 2092 | of the sequence in the calculation, and serves as a default when the\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2093 | sequence is empty."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2094 | |
| 2095 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2096 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2097 | builtin_reload(PyObject *self, PyObject *v) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2098 | { |
Benjamin Peterson | 9f4f481 | 2008-04-27 03:01:45 +0000 | [diff] [blame] | 2099 | if (PyErr_WarnPy3k("In 3.x, reload() is renamed to imp.reload()", |
Benjamin Peterson | f19a7b9 | 2008-04-27 18:40:21 +0000 | [diff] [blame] | 2100 | 1) < 0) |
Neal Norwitz | df25efe | 2007-05-23 06:58:36 +0000 | [diff] [blame] | 2101 | return NULL; |
| 2102 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2103 | return PyImport_ReloadModule(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2106 | PyDoc_STRVAR(reload_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2107 | "reload(module) -> module\n\ |
| 2108 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2109 | Reload the module. The module must have been successfully imported before."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2110 | |
| 2111 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2112 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2113 | builtin_repr(PyObject *self, PyObject *v) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2114 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2115 | return PyObject_Repr(v); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2116 | } |
| 2117 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2118 | PyDoc_STRVAR(repr_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2119 | "repr(object) -> string\n\ |
| 2120 | \n\ |
| 2121 | Return the canonical string representation of the object.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2122 | For most object types, eval(repr(object)) == object."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2123 | |
| 2124 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2125 | static PyObject * |
Georg Brandl | ccadf84 | 2006-03-31 18:54:53 +0000 | [diff] [blame] | 2126 | builtin_round(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 2127 | { |
Jeffrey Yasskin | 9871d8f | 2008-01-05 08:47:13 +0000 | [diff] [blame] | 2128 | double number; |
| 2129 | double f; |
| 2130 | int ndigits = 0; |
| 2131 | int i; |
Georg Brandl | ccadf84 | 2006-03-31 18:54:53 +0000 | [diff] [blame] | 2132 | static char *kwlist[] = {"number", "ndigits", 0}; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2133 | |
Jeffrey Yasskin | 9871d8f | 2008-01-05 08:47:13 +0000 | [diff] [blame] | 2134 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|i:round", |
| 2135 | kwlist, &number, &ndigits)) |
| 2136 | return NULL; |
| 2137 | f = 1.0; |
| 2138 | i = abs(ndigits); |
| 2139 | while (--i >= 0) |
| 2140 | f = f*10.0; |
| 2141 | if (ndigits < 0) |
| 2142 | number /= f; |
| 2143 | else |
| 2144 | number *= f; |
| 2145 | if (number >= 0.0) |
| 2146 | number = floor(number + 0.5); |
| 2147 | else |
| 2148 | number = ceil(number - 0.5); |
| 2149 | if (ndigits < 0) |
| 2150 | number *= f; |
| 2151 | else |
| 2152 | number /= f; |
| 2153 | return PyFloat_FromDouble(number); |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2156 | PyDoc_STRVAR(round_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2157 | "round(number[, ndigits]) -> floating point number\n\ |
| 2158 | \n\ |
| 2159 | Round a number to a given precision in decimal digits (default 0 digits).\n\ |
Jeffrey Yasskin | 9871d8f | 2008-01-05 08:47:13 +0000 | [diff] [blame] | 2160 | This always returns a floating point number. Precision may be negative."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2161 | |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 2162 | static PyObject * |
| 2163 | builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds) |
| 2164 | { |
| 2165 | PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs; |
| 2166 | PyObject *callable; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 2167 | static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0}; |
Neal Norwitz | 6f0d479 | 2005-12-15 06:40:36 +0000 | [diff] [blame] | 2168 | int reverse; |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 2169 | |
Neal Norwitz | 6f0d479 | 2005-12-15 06:40:36 +0000 | [diff] [blame] | 2170 | /* args 1-4 should match listsort in Objects/listobject.c */ |
Armin Rigo | 71d7e70 | 2005-09-20 18:13:03 +0000 | [diff] [blame] | 2171 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted", |
| 2172 | kwlist, &seq, &compare, &keyfunc, &reverse)) |
| 2173 | return NULL; |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 2174 | |
| 2175 | newlist = PySequence_List(seq); |
| 2176 | if (newlist == NULL) |
| 2177 | return NULL; |
| 2178 | |
| 2179 | callable = PyObject_GetAttrString(newlist, "sort"); |
| 2180 | if (callable == NULL) { |
| 2181 | Py_DECREF(newlist); |
| 2182 | return NULL; |
| 2183 | } |
Georg Brandl | 99d7e4e | 2005-08-31 22:21:15 +0000 | [diff] [blame] | 2184 | |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 2185 | newargs = PyTuple_GetSlice(args, 1, 4); |
| 2186 | if (newargs == NULL) { |
| 2187 | Py_DECREF(newlist); |
| 2188 | Py_DECREF(callable); |
| 2189 | return NULL; |
| 2190 | } |
| 2191 | |
| 2192 | v = PyObject_Call(callable, newargs, kwds); |
| 2193 | Py_DECREF(newargs); |
| 2194 | Py_DECREF(callable); |
| 2195 | if (v == NULL) { |
| 2196 | Py_DECREF(newlist); |
| 2197 | return NULL; |
| 2198 | } |
| 2199 | Py_DECREF(v); |
| 2200 | return newlist; |
| 2201 | } |
| 2202 | |
| 2203 | PyDoc_STRVAR(sorted_doc, |
| 2204 | "sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list"); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2205 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2206 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2207 | builtin_vars(PyObject *self, PyObject *args) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 2208 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2209 | PyObject *v = NULL; |
| 2210 | PyObject *d; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2211 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 2212 | if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2213 | return NULL; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 2214 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2215 | d = PyEval_GetLocals(); |
Guido van Rossum | 53bb7ff | 1995-07-26 16:26:31 +0000 | [diff] [blame] | 2216 | if (d == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2217 | if (!PyErr_Occurred()) |
| 2218 | PyErr_SetString(PyExc_SystemError, |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 2219 | "vars(): no locals!?"); |
Guido van Rossum | 53bb7ff | 1995-07-26 16:26:31 +0000 | [diff] [blame] | 2220 | } |
| 2221 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2222 | Py_INCREF(d); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 2223 | } |
| 2224 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2225 | d = PyObject_GetAttrString(v, "__dict__"); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 2226 | if (d == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2227 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 2228 | "vars() argument must have __dict__ attribute"); |
| 2229 | return NULL; |
| 2230 | } |
| 2231 | } |
| 2232 | return d; |
| 2233 | } |
| 2234 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2235 | PyDoc_STRVAR(vars_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2236 | "vars([object]) -> dictionary\n\ |
| 2237 | \n\ |
| 2238 | Without arguments, equivalent to locals().\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2239 | With an argument, equivalent to object.__dict__."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2240 | |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 2241 | |
| 2242 | static PyObject* |
| 2243 | builtin_sum(PyObject *self, PyObject *args) |
| 2244 | { |
| 2245 | PyObject *seq; |
| 2246 | PyObject *result = NULL; |
| 2247 | PyObject *temp, *item, *iter; |
| 2248 | |
Raymond Hettinger | 5cf6394 | 2003-10-25 06:41:37 +0000 | [diff] [blame] | 2249 | if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result)) |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 2250 | return NULL; |
| 2251 | |
| 2252 | iter = PyObject_GetIter(seq); |
| 2253 | if (iter == NULL) |
| 2254 | return NULL; |
| 2255 | |
| 2256 | if (result == NULL) { |
| 2257 | result = PyInt_FromLong(0); |
| 2258 | if (result == NULL) { |
| 2259 | Py_DECREF(iter); |
| 2260 | return NULL; |
| 2261 | } |
| 2262 | } else { |
| 2263 | /* reject string values for 'start' parameter */ |
| 2264 | if (PyObject_TypeCheck(result, &PyBaseString_Type)) { |
| 2265 | PyErr_SetString(PyExc_TypeError, |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 2266 | "sum() can't sum strings [use ''.join(seq) instead]"); |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 2267 | Py_DECREF(iter); |
| 2268 | return NULL; |
| 2269 | } |
Alex Martelli | 41c9f88 | 2003-04-22 09:24:48 +0000 | [diff] [blame] | 2270 | Py_INCREF(result); |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
Raymond Hettinger | 3f8caa3 | 2007-10-24 01:28:33 +0000 | [diff] [blame] | 2273 | #ifndef SLOW_SUM |
| 2274 | /* Fast addition by keeping temporary sums in C instead of new Python objects. |
| 2275 | Assumes all inputs are the same type. If the assumption fails, default |
| 2276 | to the more general routine. |
| 2277 | */ |
| 2278 | if (PyInt_CheckExact(result)) { |
| 2279 | long i_result = PyInt_AS_LONG(result); |
| 2280 | Py_DECREF(result); |
| 2281 | result = NULL; |
| 2282 | while(result == NULL) { |
| 2283 | item = PyIter_Next(iter); |
| 2284 | if (item == NULL) { |
| 2285 | Py_DECREF(iter); |
| 2286 | if (PyErr_Occurred()) |
| 2287 | return NULL; |
| 2288 | return PyInt_FromLong(i_result); |
| 2289 | } |
| 2290 | if (PyInt_CheckExact(item)) { |
| 2291 | long b = PyInt_AS_LONG(item); |
| 2292 | long x = i_result + b; |
| 2293 | if ((x^i_result) >= 0 || (x^b) >= 0) { |
| 2294 | i_result = x; |
| 2295 | Py_DECREF(item); |
| 2296 | continue; |
| 2297 | } |
| 2298 | } |
| 2299 | /* Either overflowed or is not an int. Restore real objects and process normally */ |
| 2300 | result = PyInt_FromLong(i_result); |
| 2301 | temp = PyNumber_Add(result, item); |
| 2302 | Py_DECREF(result); |
| 2303 | Py_DECREF(item); |
| 2304 | result = temp; |
| 2305 | if (result == NULL) { |
| 2306 | Py_DECREF(iter); |
| 2307 | return NULL; |
| 2308 | } |
| 2309 | } |
| 2310 | } |
| 2311 | |
| 2312 | if (PyFloat_CheckExact(result)) { |
| 2313 | double f_result = PyFloat_AS_DOUBLE(result); |
| 2314 | Py_DECREF(result); |
| 2315 | result = NULL; |
| 2316 | while(result == NULL) { |
| 2317 | item = PyIter_Next(iter); |
| 2318 | if (item == NULL) { |
| 2319 | Py_DECREF(iter); |
| 2320 | if (PyErr_Occurred()) |
| 2321 | return NULL; |
| 2322 | return PyFloat_FromDouble(f_result); |
| 2323 | } |
| 2324 | if (PyFloat_CheckExact(item)) { |
Raymond Hettinger | 6585660 | 2008-05-30 06:37:27 +0000 | [diff] [blame] | 2325 | PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0) |
Raymond Hettinger | 3f8caa3 | 2007-10-24 01:28:33 +0000 | [diff] [blame] | 2326 | f_result += PyFloat_AS_DOUBLE(item); |
Raymond Hettinger | 3a8daf5 | 2007-10-24 02:05:51 +0000 | [diff] [blame] | 2327 | PyFPE_END_PROTECT(f_result) |
Raymond Hettinger | a45c487 | 2007-10-25 02:26:58 +0000 | [diff] [blame] | 2328 | Py_DECREF(item); |
Raymond Hettinger | 3a8daf5 | 2007-10-24 02:05:51 +0000 | [diff] [blame] | 2329 | continue; |
| 2330 | } |
| 2331 | if (PyInt_CheckExact(item)) { |
Raymond Hettinger | 6585660 | 2008-05-30 06:37:27 +0000 | [diff] [blame] | 2332 | PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0) |
Raymond Hettinger | 3a8daf5 | 2007-10-24 02:05:51 +0000 | [diff] [blame] | 2333 | f_result += (double)PyInt_AS_LONG(item); |
| 2334 | PyFPE_END_PROTECT(f_result) |
Raymond Hettinger | a45c487 | 2007-10-25 02:26:58 +0000 | [diff] [blame] | 2335 | Py_DECREF(item); |
Raymond Hettinger | 3f8caa3 | 2007-10-24 01:28:33 +0000 | [diff] [blame] | 2336 | continue; |
| 2337 | } |
| 2338 | result = PyFloat_FromDouble(f_result); |
| 2339 | temp = PyNumber_Add(result, item); |
| 2340 | Py_DECREF(result); |
| 2341 | Py_DECREF(item); |
| 2342 | result = temp; |
| 2343 | if (result == NULL) { |
| 2344 | Py_DECREF(iter); |
| 2345 | return NULL; |
| 2346 | } |
| 2347 | } |
| 2348 | } |
| 2349 | #endif |
| 2350 | |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 2351 | for(;;) { |
| 2352 | item = PyIter_Next(iter); |
| 2353 | if (item == NULL) { |
| 2354 | /* error, or end-of-sequence */ |
| 2355 | if (PyErr_Occurred()) { |
| 2356 | Py_DECREF(result); |
| 2357 | result = NULL; |
| 2358 | } |
| 2359 | break; |
| 2360 | } |
Alex Martelli | a253e18 | 2003-10-25 23:24:14 +0000 | [diff] [blame] | 2361 | temp = PyNumber_Add(result, item); |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 2362 | Py_DECREF(result); |
| 2363 | Py_DECREF(item); |
| 2364 | result = temp; |
| 2365 | if (result == NULL) |
| 2366 | break; |
| 2367 | } |
| 2368 | Py_DECREF(iter); |
| 2369 | return result; |
| 2370 | } |
| 2371 | |
| 2372 | PyDoc_STRVAR(sum_doc, |
Georg Brandl | 8134d06 | 2006-10-12 12:33:07 +0000 | [diff] [blame] | 2373 | "sum(sequence[, start]) -> value\n\ |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 2374 | \n\ |
| 2375 | Returns the sum of a sequence of numbers (NOT strings) plus the value\n\ |
Georg Brandl | 8134d06 | 2006-10-12 12:33:07 +0000 | [diff] [blame] | 2376 | of parameter 'start' (which defaults to 0). When the sequence is\n\ |
| 2377 | empty, returns start."); |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 2378 | |
| 2379 | |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 2380 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2381 | builtin_isinstance(PyObject *self, PyObject *args) |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 2382 | { |
| 2383 | PyObject *inst; |
| 2384 | PyObject *cls; |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2385 | int retval; |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 2386 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 2387 | if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls)) |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 2388 | return NULL; |
Guido van Rossum | f5dd914 | 1997-12-02 19:11:45 +0000 | [diff] [blame] | 2389 | |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2390 | retval = PyObject_IsInstance(inst, cls); |
| 2391 | if (retval < 0) |
Guido van Rossum | ad99177 | 2001-01-12 16:03:05 +0000 | [diff] [blame] | 2392 | return NULL; |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 2393 | return PyBool_FromLong(retval); |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 2394 | } |
| 2395 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2396 | PyDoc_STRVAR(isinstance_doc, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 2397 | "isinstance(object, class-or-type-or-tuple) -> bool\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2398 | \n\ |
| 2399 | Return whether an object is an instance of a class or of a subclass thereof.\n\ |
Guido van Rossum | 03290ec | 2001-10-07 20:54:12 +0000 | [diff] [blame] | 2400 | With a type as second argument, return whether that is the object's type.\n\ |
| 2401 | The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2402 | isinstance(x, A) or isinstance(x, B) or ... (etc.)."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2403 | |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 2404 | |
| 2405 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2406 | builtin_issubclass(PyObject *self, PyObject *args) |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 2407 | { |
| 2408 | PyObject *derived; |
| 2409 | PyObject *cls; |
| 2410 | int retval; |
| 2411 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 2412 | if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls)) |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 2413 | return NULL; |
Guido van Rossum | 668213d | 1999-06-16 17:28:37 +0000 | [diff] [blame] | 2414 | |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 2415 | retval = PyObject_IsSubclass(derived, cls); |
| 2416 | if (retval < 0) |
| 2417 | return NULL; |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 2418 | return PyBool_FromLong(retval); |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2421 | PyDoc_STRVAR(issubclass_doc, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 2422 | "issubclass(C, B) -> bool\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2423 | \n\ |
Walter Dörwald | d9a6ad3 | 2002-12-12 16:41:44 +0000 | [diff] [blame] | 2424 | Return whether class C is a subclass (i.e., a derived class) of class B.\n\ |
| 2425 | When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\ |
| 2426 | is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.)."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2427 | |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 2428 | |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2429 | static PyObject* |
| 2430 | builtin_zip(PyObject *self, PyObject *args) |
| 2431 | { |
| 2432 | PyObject *ret; |
Martin v. Löwis | d96ee90 | 2006-02-16 14:37:16 +0000 | [diff] [blame] | 2433 | const Py_ssize_t itemsize = PySequence_Length(args); |
| 2434 | Py_ssize_t i; |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 2435 | PyObject *itlist; /* tuple of iterators */ |
Martin v. Löwis | d96ee90 | 2006-02-16 14:37:16 +0000 | [diff] [blame] | 2436 | Py_ssize_t len; /* guess at result length */ |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2437 | |
Raymond Hettinger | eaef615 | 2003-08-02 07:42:57 +0000 | [diff] [blame] | 2438 | if (itemsize == 0) |
| 2439 | return PyList_New(0); |
| 2440 | |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2441 | /* args must be a tuple */ |
| 2442 | assert(PyTuple_Check(args)); |
| 2443 | |
Tim Peters | 39a86c2 | 2002-05-12 07:19:38 +0000 | [diff] [blame] | 2444 | /* Guess at result length: the shortest of the input lengths. |
| 2445 | If some argument refuses to say, we refuse to guess too, lest |
| 2446 | an argument like xrange(sys.maxint) lead us astray.*/ |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 2447 | len = -1; /* unknown */ |
| 2448 | for (i = 0; i < itemsize; ++i) { |
| 2449 | PyObject *item = PyTuple_GET_ITEM(args, i); |
Raymond Hettinger | 4e2f714 | 2007-12-06 00:56:53 +0000 | [diff] [blame] | 2450 | Py_ssize_t thislen = _PyObject_LengthHint(item, -1); |
Tim Peters | 39a86c2 | 2002-05-12 07:19:38 +0000 | [diff] [blame] | 2451 | if (thislen < 0) { |
Tim Peters | 39a86c2 | 2002-05-12 07:19:38 +0000 | [diff] [blame] | 2452 | len = -1; |
| 2453 | break; |
| 2454 | } |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 2455 | else if (len < 0 || thislen < len) |
| 2456 | len = thislen; |
| 2457 | } |
| 2458 | |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 2459 | /* allocate result list */ |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 2460 | if (len < 0) |
| 2461 | len = 10; /* arbitrary */ |
| 2462 | if ((ret = PyList_New(len)) == NULL) |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2463 | return NULL; |
| 2464 | |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 2465 | /* obtain iterators */ |
| 2466 | itlist = PyTuple_New(itemsize); |
| 2467 | if (itlist == NULL) |
| 2468 | goto Fail_ret; |
| 2469 | for (i = 0; i < itemsize; ++i) { |
| 2470 | PyObject *item = PyTuple_GET_ITEM(args, i); |
| 2471 | PyObject *it = PyObject_GetIter(item); |
| 2472 | if (it == NULL) { |
| 2473 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 2474 | PyErr_Format(PyExc_TypeError, |
Neal Norwitz | a361bd8 | 2006-02-19 19:31:50 +0000 | [diff] [blame] | 2475 | "zip argument #%zd must support iteration", |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 2476 | i+1); |
| 2477 | goto Fail_ret_itlist; |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2478 | } |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 2479 | PyTuple_SET_ITEM(itlist, i, it); |
| 2480 | } |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2481 | |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 2482 | /* build result into ret list */ |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 2483 | for (i = 0; ; ++i) { |
| 2484 | int j; |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 2485 | PyObject *next = PyTuple_New(itemsize); |
| 2486 | if (!next) |
| 2487 | goto Fail_ret_itlist; |
| 2488 | |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 2489 | for (j = 0; j < itemsize; j++) { |
| 2490 | PyObject *it = PyTuple_GET_ITEM(itlist, j); |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 2491 | PyObject *item = PyIter_Next(it); |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2492 | if (!item) { |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 2493 | if (PyErr_Occurred()) { |
| 2494 | Py_DECREF(ret); |
| 2495 | ret = NULL; |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2496 | } |
| 2497 | Py_DECREF(next); |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 2498 | Py_DECREF(itlist); |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 2499 | goto Done; |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2500 | } |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 2501 | PyTuple_SET_ITEM(next, j, item); |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2502 | } |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 2503 | |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 2504 | if (i < len) |
| 2505 | PyList_SET_ITEM(ret, i, next); |
| 2506 | else { |
| 2507 | int status = PyList_Append(ret, next); |
| 2508 | Py_DECREF(next); |
| 2509 | ++len; |
| 2510 | if (status < 0) |
| 2511 | goto Fail_ret_itlist; |
| 2512 | } |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2513 | } |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 2514 | |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 2515 | Done: |
| 2516 | if (ret != NULL && i < len) { |
| 2517 | /* The list is too big. */ |
| 2518 | if (PyList_SetSlice(ret, i, len, NULL) < 0) |
| 2519 | return NULL; |
| 2520 | } |
| 2521 | return ret; |
| 2522 | |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 2523 | Fail_ret_itlist: |
| 2524 | Py_DECREF(itlist); |
| 2525 | Fail_ret: |
| 2526 | Py_DECREF(ret); |
| 2527 | return NULL; |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
| 2530 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2531 | PyDoc_STRVAR(zip_doc, |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2532 | "zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\ |
| 2533 | \n\ |
| 2534 | Return a list of tuples, where each tuple contains the i-th element\n\ |
| 2535 | from each of the argument sequences. The returned list is truncated\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2536 | in length to the length of the shortest argument sequence."); |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2537 | |
| 2538 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2539 | static PyMethodDef builtin_methods[] = { |
Neal Norwitz | 92e212f | 2006-04-03 04:48:37 +0000 | [diff] [blame] | 2540 | {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2541 | {"abs", builtin_abs, METH_O, abs_doc}, |
Raymond Hettinger | 96229b1 | 2005-03-11 06:49:40 +0000 | [diff] [blame] | 2542 | {"all", builtin_all, METH_O, all_doc}, |
| 2543 | {"any", builtin_any, METH_O, any_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2544 | {"apply", builtin_apply, METH_VARARGS, apply_doc}, |
Eric Smith | 3cd8194 | 2008-02-22 16:30:22 +0000 | [diff] [blame] | 2545 | {"bin", builtin_bin, METH_O, bin_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2546 | {"callable", builtin_callable, METH_O, callable_doc}, |
| 2547 | {"chr", builtin_chr, METH_VARARGS, chr_doc}, |
| 2548 | {"cmp", builtin_cmp, METH_VARARGS, cmp_doc}, |
| 2549 | {"coerce", builtin_coerce, METH_VARARGS, coerce_doc}, |
Georg Brandl | 5240d74 | 2007-03-13 20:46:32 +0000 | [diff] [blame] | 2550 | {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2551 | {"delattr", builtin_delattr, METH_VARARGS, delattr_doc}, |
| 2552 | {"dir", builtin_dir, METH_VARARGS, dir_doc}, |
| 2553 | {"divmod", builtin_divmod, METH_VARARGS, divmod_doc}, |
| 2554 | {"eval", builtin_eval, METH_VARARGS, eval_doc}, |
| 2555 | {"execfile", builtin_execfile, METH_VARARGS, execfile_doc}, |
| 2556 | {"filter", builtin_filter, METH_VARARGS, filter_doc}, |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 2557 | {"format", builtin_format, METH_VARARGS, format_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2558 | {"getattr", builtin_getattr, METH_VARARGS, getattr_doc}, |
| 2559 | {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc}, |
| 2560 | {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc}, |
| 2561 | {"hash", builtin_hash, METH_O, hash_doc}, |
| 2562 | {"hex", builtin_hex, METH_O, hex_doc}, |
| 2563 | {"id", builtin_id, METH_O, id_doc}, |
| 2564 | {"input", builtin_input, METH_VARARGS, input_doc}, |
| 2565 | {"intern", builtin_intern, METH_VARARGS, intern_doc}, |
| 2566 | {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc}, |
| 2567 | {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc}, |
| 2568 | {"iter", builtin_iter, METH_VARARGS, iter_doc}, |
| 2569 | {"len", builtin_len, METH_O, len_doc}, |
| 2570 | {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc}, |
| 2571 | {"map", builtin_map, METH_VARARGS, map_doc}, |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 2572 | {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc}, |
| 2573 | {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc}, |
Georg Brandl | 28e0873 | 2008-04-30 19:47:09 +0000 | [diff] [blame] | 2574 | {"next", builtin_next, METH_VARARGS, next_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2575 | {"oct", builtin_oct, METH_O, oct_doc}, |
Neal Norwitz | c4edb0e | 2006-05-02 04:43:14 +0000 | [diff] [blame] | 2576 | {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2577 | {"ord", builtin_ord, METH_O, ord_doc}, |
| 2578 | {"pow", builtin_pow, METH_VARARGS, pow_doc}, |
Eric Smith | 7c47894 | 2008-03-18 23:45:49 +0000 | [diff] [blame] | 2579 | {"print", (PyCFunction)builtin_print, METH_VARARGS | METH_KEYWORDS, print_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2580 | {"range", builtin_range, METH_VARARGS, range_doc}, |
| 2581 | {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc}, |
| 2582 | {"reduce", builtin_reduce, METH_VARARGS, reduce_doc}, |
| 2583 | {"reload", builtin_reload, METH_O, reload_doc}, |
| 2584 | {"repr", builtin_repr, METH_O, repr_doc}, |
Georg Brandl | ccadf84 | 2006-03-31 18:54:53 +0000 | [diff] [blame] | 2585 | {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2586 | {"setattr", builtin_setattr, METH_VARARGS, setattr_doc}, |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 2587 | {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc}, |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 2588 | {"sum", builtin_sum, METH_VARARGS, sum_doc}, |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2589 | #ifdef Py_USING_UNICODE |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2590 | {"unichr", builtin_unichr, METH_VARARGS, unichr_doc}, |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2591 | #endif |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2592 | {"vars", builtin_vars, METH_VARARGS, vars_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2593 | {"zip", builtin_zip, METH_VARARGS, zip_doc}, |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 2594 | {NULL, NULL}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2595 | }; |
| 2596 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2597 | PyDoc_STRVAR(builtin_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2598 | "Built-in functions, exceptions, and other objects.\n\ |
| 2599 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2600 | Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2601 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2602 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2603 | _PyBuiltin_Init(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2604 | { |
Fred Drake | 5550de3 | 2000-06-20 04:54:19 +0000 | [diff] [blame] | 2605 | PyObject *mod, *dict, *debug; |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2606 | mod = Py_InitModule4("__builtin__", builtin_methods, |
| 2607 | builtin_doc, (PyObject *)NULL, |
| 2608 | PYTHON_API_VERSION); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2609 | if (mod == NULL) |
| 2610 | return NULL; |
| 2611 | dict = PyModule_GetDict(mod); |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2612 | |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 2613 | #ifdef Py_TRACE_REFS |
| 2614 | /* __builtin__ exposes a number of statically allocated objects |
| 2615 | * that, before this code was added in 2.3, never showed up in |
| 2616 | * the list of "all objects" maintained by Py_TRACE_REFS. As a |
| 2617 | * result, programs leaking references to None and False (etc) |
| 2618 | * couldn't be diagnosed by examining sys.getobjects(0). |
| 2619 | */ |
| 2620 | #define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0) |
| 2621 | #else |
| 2622 | #define ADD_TO_ALL(OBJECT) (void)0 |
| 2623 | #endif |
| 2624 | |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2625 | #define SETBUILTIN(NAME, OBJECT) \ |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 2626 | if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \ |
| 2627 | return NULL; \ |
| 2628 | ADD_TO_ALL(OBJECT) |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2629 | |
| 2630 | SETBUILTIN("None", Py_None); |
| 2631 | SETBUILTIN("Ellipsis", Py_Ellipsis); |
| 2632 | SETBUILTIN("NotImplemented", Py_NotImplemented); |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 2633 | SETBUILTIN("False", Py_False); |
| 2634 | SETBUILTIN("True", Py_True); |
Neal Norwitz | 32a7e7f | 2002-05-31 19:58:02 +0000 | [diff] [blame] | 2635 | SETBUILTIN("basestring", &PyBaseString_Type); |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 2636 | SETBUILTIN("bool", &PyBool_Type); |
Travis E. Oliphant | 3781aef | 2008-03-18 04:44:57 +0000 | [diff] [blame] | 2637 | /* SETBUILTIN("memoryview", &PyMemoryView_Type); */ |
Christian Heimes | 3497f94 | 2008-05-26 12:29:14 +0000 | [diff] [blame] | 2638 | SETBUILTIN("bytearray", &PyByteArray_Type); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2639 | SETBUILTIN("bytes", &PyString_Type); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 2640 | SETBUILTIN("buffer", &PyBuffer_Type); |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2641 | SETBUILTIN("classmethod", &PyClassMethod_Type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2642 | #ifndef WITHOUT_COMPLEX |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2643 | SETBUILTIN("complex", &PyComplex_Type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2644 | #endif |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 2645 | SETBUILTIN("dict", &PyDict_Type); |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 2646 | SETBUILTIN("enumerate", &PyEnum_Type); |
Neal Norwitz | c4edb0e | 2006-05-02 04:43:14 +0000 | [diff] [blame] | 2647 | SETBUILTIN("file", &PyFile_Type); |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2648 | SETBUILTIN("float", &PyFloat_Type); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2649 | SETBUILTIN("frozenset", &PyFrozenSet_Type); |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2650 | SETBUILTIN("property", &PyProperty_Type); |
| 2651 | SETBUILTIN("int", &PyInt_Type); |
| 2652 | SETBUILTIN("list", &PyList_Type); |
| 2653 | SETBUILTIN("long", &PyLong_Type); |
| 2654 | SETBUILTIN("object", &PyBaseObject_Type); |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 2655 | SETBUILTIN("reversed", &PyReversed_Type); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2656 | SETBUILTIN("set", &PySet_Type); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 2657 | SETBUILTIN("slice", &PySlice_Type); |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2658 | SETBUILTIN("staticmethod", &PyStaticMethod_Type); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2659 | SETBUILTIN("str", &PyString_Type); |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2660 | SETBUILTIN("super", &PySuper_Type); |
| 2661 | SETBUILTIN("tuple", &PyTuple_Type); |
| 2662 | SETBUILTIN("type", &PyType_Type); |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 2663 | SETBUILTIN("xrange", &PyRange_Type); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2664 | #ifdef Py_USING_UNICODE |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2665 | SETBUILTIN("unicode", &PyUnicode_Type); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2666 | #endif |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 2667 | debug = PyBool_FromLong(Py_OptimizeFlag == 0); |
Fred Drake | 5550de3 | 2000-06-20 04:54:19 +0000 | [diff] [blame] | 2668 | if (PyDict_SetItemString(dict, "__debug__", debug) < 0) { |
| 2669 | Py_XDECREF(debug); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2670 | return NULL; |
Fred Drake | 5550de3 | 2000-06-20 04:54:19 +0000 | [diff] [blame] | 2671 | } |
| 2672 | Py_XDECREF(debug); |
Barry Warsaw | 757af0e | 1997-08-29 22:13:51 +0000 | [diff] [blame] | 2673 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2674 | return mod; |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 2675 | #undef ADD_TO_ALL |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2676 | #undef SETBUILTIN |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2677 | } |
| 2678 | |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 2679 | /* Helper for filter(): filter a tuple through a function */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2680 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2681 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2682 | filtertuple(PyObject *func, PyObject *tuple) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2683 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2684 | PyObject *result; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2685 | Py_ssize_t i, j; |
| 2686 | Py_ssize_t len = PyTuple_Size(tuple); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2687 | |
Guido van Rossum | b7b4562 | 1995-08-04 04:07:45 +0000 | [diff] [blame] | 2688 | if (len == 0) { |
Walter Dörwald | c3da83f | 2003-02-04 20:24:45 +0000 | [diff] [blame] | 2689 | if (PyTuple_CheckExact(tuple)) |
| 2690 | Py_INCREF(tuple); |
| 2691 | else |
| 2692 | tuple = PyTuple_New(0); |
Guido van Rossum | b7b4562 | 1995-08-04 04:07:45 +0000 | [diff] [blame] | 2693 | return tuple; |
| 2694 | } |
| 2695 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2696 | if ((result = PyTuple_New(len)) == NULL) |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 2697 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2698 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2699 | for (i = j = 0; i < len; ++i) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2700 | PyObject *item, *good; |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2701 | int ok; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2702 | |
Walter Dörwald | 8dd1932 | 2003-02-10 17:36:40 +0000 | [diff] [blame] | 2703 | if (tuple->ob_type->tp_as_sequence && |
| 2704 | tuple->ob_type->tp_as_sequence->sq_item) { |
| 2705 | item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i); |
Walter Dörwald | c58a3a1 | 2003-08-18 18:28:45 +0000 | [diff] [blame] | 2706 | if (item == NULL) |
| 2707 | goto Fail_1; |
Walter Dörwald | 8dd1932 | 2003-02-10 17:36:40 +0000 | [diff] [blame] | 2708 | } else { |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 2709 | PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple"); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2710 | goto Fail_1; |
Walter Dörwald | 8dd1932 | 2003-02-10 17:36:40 +0000 | [diff] [blame] | 2711 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2712 | if (func == Py_None) { |
| 2713 | Py_INCREF(item); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2714 | good = item; |
| 2715 | } |
| 2716 | else { |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 2717 | PyObject *arg = PyTuple_Pack(1, item); |
Walter Dörwald | c58a3a1 | 2003-08-18 18:28:45 +0000 | [diff] [blame] | 2718 | if (arg == NULL) { |
| 2719 | Py_DECREF(item); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2720 | goto Fail_1; |
Walter Dörwald | c58a3a1 | 2003-08-18 18:28:45 +0000 | [diff] [blame] | 2721 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2722 | good = PyEval_CallObject(func, arg); |
| 2723 | Py_DECREF(arg); |
Walter Dörwald | c58a3a1 | 2003-08-18 18:28:45 +0000 | [diff] [blame] | 2724 | if (good == NULL) { |
| 2725 | Py_DECREF(item); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2726 | goto Fail_1; |
Walter Dörwald | c58a3a1 | 2003-08-18 18:28:45 +0000 | [diff] [blame] | 2727 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2728 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2729 | ok = PyObject_IsTrue(good); |
| 2730 | Py_DECREF(good); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2731 | if (ok) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2732 | if (PyTuple_SetItem(result, j++, item) < 0) |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2733 | goto Fail_1; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2734 | } |
Walter Dörwald | c58a3a1 | 2003-08-18 18:28:45 +0000 | [diff] [blame] | 2735 | else |
| 2736 | Py_DECREF(item); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
Tim Peters | 4324aa3 | 2001-05-28 22:30:08 +0000 | [diff] [blame] | 2739 | if (_PyTuple_Resize(&result, j) < 0) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2740 | return NULL; |
| 2741 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2742 | return result; |
| 2743 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2744 | Fail_1: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2745 | Py_DECREF(result); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2746 | return NULL; |
| 2747 | } |
| 2748 | |
| 2749 | |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 2750 | /* Helper for filter(): filter a string through a function */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2751 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2752 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2753 | filterstring(PyObject *func, PyObject *strobj) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2754 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2755 | PyObject *result; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2756 | Py_ssize_t i, j; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2757 | Py_ssize_t len = PyString_Size(strobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2758 | Py_ssize_t outlen = len; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2759 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2760 | if (func == Py_None) { |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 2761 | /* If it's a real string we can return the original, |
| 2762 | * as no character is ever false and __getitem__ |
| 2763 | * does return this character. If it's a subclass |
| 2764 | * we must go through the __getitem__ loop */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2765 | if (PyString_CheckExact(strobj)) { |
Walter Dörwald | c3da83f | 2003-02-04 20:24:45 +0000 | [diff] [blame] | 2766 | Py_INCREF(strobj); |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 2767 | return strobj; |
| 2768 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2769 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2770 | if ((result = PyString_FromStringAndSize(NULL, len)) == NULL) |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 2771 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2772 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2773 | for (i = j = 0; i < len; ++i) { |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 2774 | PyObject *item; |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2775 | int ok; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2776 | |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2777 | item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i); |
| 2778 | if (item == NULL) |
| 2779 | goto Fail_1; |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 2780 | if (func==Py_None) { |
| 2781 | ok = 1; |
| 2782 | } else { |
| 2783 | PyObject *arg, *good; |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 2784 | arg = PyTuple_Pack(1, item); |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 2785 | if (arg == NULL) { |
| 2786 | Py_DECREF(item); |
| 2787 | goto Fail_1; |
| 2788 | } |
| 2789 | good = PyEval_CallObject(func, arg); |
| 2790 | Py_DECREF(arg); |
| 2791 | if (good == NULL) { |
| 2792 | Py_DECREF(item); |
| 2793 | goto Fail_1; |
| 2794 | } |
| 2795 | ok = PyObject_IsTrue(good); |
| 2796 | Py_DECREF(good); |
Tim Peters | 388ed08 | 2001-04-07 20:34:48 +0000 | [diff] [blame] | 2797 | } |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2798 | if (ok) { |
Martin v. Löwis | d96ee90 | 2006-02-16 14:37:16 +0000 | [diff] [blame] | 2799 | Py_ssize_t reslen; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2800 | if (!PyString_Check(item)) { |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2801 | PyErr_SetString(PyExc_TypeError, "can't filter str to str:" |
| 2802 | " __getitem__ returned different type"); |
| 2803 | Py_DECREF(item); |
| 2804 | goto Fail_1; |
| 2805 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2806 | reslen = PyString_GET_SIZE(item); |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2807 | if (reslen == 1) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2808 | PyString_AS_STRING(result)[j++] = |
| 2809 | PyString_AS_STRING(item)[0]; |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2810 | } else { |
| 2811 | /* do we need more space? */ |
Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 2812 | Py_ssize_t need = j; |
| 2813 | |
| 2814 | /* calculate space requirements while checking for overflow */ |
| 2815 | if (need > PY_SSIZE_T_MAX - reslen) { |
| 2816 | Py_DECREF(item); |
| 2817 | goto Fail_1; |
| 2818 | } |
| 2819 | |
| 2820 | need += reslen; |
| 2821 | |
| 2822 | if (need > PY_SSIZE_T_MAX - len) { |
| 2823 | Py_DECREF(item); |
| 2824 | goto Fail_1; |
| 2825 | } |
| 2826 | |
| 2827 | need += len; |
| 2828 | |
| 2829 | if (need <= i) { |
| 2830 | Py_DECREF(item); |
| 2831 | goto Fail_1; |
| 2832 | } |
| 2833 | |
| 2834 | need = need - i - 1; |
| 2835 | |
| 2836 | assert(need >= 0); |
| 2837 | assert(outlen >= 0); |
| 2838 | |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2839 | if (need > outlen) { |
| 2840 | /* overallocate, to avoid reallocations */ |
Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 2841 | if (outlen > PY_SSIZE_T_MAX / 2) { |
| 2842 | Py_DECREF(item); |
| 2843 | return NULL; |
| 2844 | } |
| 2845 | |
| 2846 | if (need<2*outlen) { |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2847 | need = 2*outlen; |
Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 2848 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2849 | if (_PyString_Resize(&result, need)) { |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2850 | Py_DECREF(item); |
| 2851 | return NULL; |
| 2852 | } |
| 2853 | outlen = need; |
| 2854 | } |
| 2855 | memcpy( |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2856 | PyString_AS_STRING(result) + j, |
| 2857 | PyString_AS_STRING(item), |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2858 | reslen |
| 2859 | ); |
| 2860 | j += reslen; |
| 2861 | } |
| 2862 | } |
Tim Peters | 388ed08 | 2001-04-07 20:34:48 +0000 | [diff] [blame] | 2863 | Py_DECREF(item); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2864 | } |
| 2865 | |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2866 | if (j < outlen) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2867 | _PyString_Resize(&result, j); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2868 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2869 | return result; |
| 2870 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2871 | Fail_1: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2872 | Py_DECREF(result); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2873 | return NULL; |
| 2874 | } |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 2875 | |
| 2876 | #ifdef Py_USING_UNICODE |
| 2877 | /* Helper for filter(): filter a Unicode object through a function */ |
| 2878 | |
| 2879 | static PyObject * |
| 2880 | filterunicode(PyObject *func, PyObject *strobj) |
| 2881 | { |
| 2882 | PyObject *result; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 2883 | register Py_ssize_t i, j; |
Martin v. Löwis | d96ee90 | 2006-02-16 14:37:16 +0000 | [diff] [blame] | 2884 | Py_ssize_t len = PyUnicode_GetSize(strobj); |
| 2885 | Py_ssize_t outlen = len; |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 2886 | |
| 2887 | if (func == Py_None) { |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 2888 | /* If it's a real string we can return the original, |
| 2889 | * as no character is ever false and __getitem__ |
| 2890 | * does return this character. If it's a subclass |
| 2891 | * we must go through the __getitem__ loop */ |
| 2892 | if (PyUnicode_CheckExact(strobj)) { |
Walter Dörwald | c3da83f | 2003-02-04 20:24:45 +0000 | [diff] [blame] | 2893 | Py_INCREF(strobj); |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 2894 | return strobj; |
| 2895 | } |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 2896 | } |
| 2897 | if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL) |
| 2898 | return NULL; |
| 2899 | |
| 2900 | for (i = j = 0; i < len; ++i) { |
| 2901 | PyObject *item, *arg, *good; |
| 2902 | int ok; |
| 2903 | |
| 2904 | item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i); |
| 2905 | if (item == NULL) |
| 2906 | goto Fail_1; |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 2907 | if (func == Py_None) { |
| 2908 | ok = 1; |
| 2909 | } else { |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 2910 | arg = PyTuple_Pack(1, item); |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 2911 | if (arg == NULL) { |
| 2912 | Py_DECREF(item); |
| 2913 | goto Fail_1; |
| 2914 | } |
| 2915 | good = PyEval_CallObject(func, arg); |
| 2916 | Py_DECREF(arg); |
| 2917 | if (good == NULL) { |
| 2918 | Py_DECREF(item); |
| 2919 | goto Fail_1; |
| 2920 | } |
| 2921 | ok = PyObject_IsTrue(good); |
| 2922 | Py_DECREF(good); |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 2923 | } |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2924 | if (ok) { |
Martin v. Löwis | d96ee90 | 2006-02-16 14:37:16 +0000 | [diff] [blame] | 2925 | Py_ssize_t reslen; |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2926 | if (!PyUnicode_Check(item)) { |
Georg Brandl | 99d7e4e | 2005-08-31 22:21:15 +0000 | [diff] [blame] | 2927 | PyErr_SetString(PyExc_TypeError, |
Jeremy Hylton | 1aad9c7 | 2003-09-16 03:10:59 +0000 | [diff] [blame] | 2928 | "can't filter unicode to unicode:" |
| 2929 | " __getitem__ returned different type"); |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2930 | Py_DECREF(item); |
| 2931 | goto Fail_1; |
| 2932 | } |
| 2933 | reslen = PyUnicode_GET_SIZE(item); |
Georg Brandl | 99d7e4e | 2005-08-31 22:21:15 +0000 | [diff] [blame] | 2934 | if (reslen == 1) |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2935 | PyUnicode_AS_UNICODE(result)[j++] = |
| 2936 | PyUnicode_AS_UNICODE(item)[0]; |
Jeremy Hylton | 1aad9c7 | 2003-09-16 03:10:59 +0000 | [diff] [blame] | 2937 | else { |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2938 | /* do we need more space? */ |
Martin v. Löwis | d96ee90 | 2006-02-16 14:37:16 +0000 | [diff] [blame] | 2939 | Py_ssize_t need = j + reslen + len - i - 1; |
Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 2940 | |
| 2941 | /* check that didnt overflow */ |
| 2942 | if ((j > PY_SSIZE_T_MAX - reslen) || |
| 2943 | ((j + reslen) > PY_SSIZE_T_MAX - len) || |
| 2944 | ((j + reslen + len) < i) || |
| 2945 | ((j + reslen + len - i) <= 0)) { |
| 2946 | Py_DECREF(item); |
| 2947 | return NULL; |
| 2948 | } |
| 2949 | |
| 2950 | assert(need >= 0); |
| 2951 | assert(outlen >= 0); |
| 2952 | |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2953 | if (need > outlen) { |
Georg Brandl | 99d7e4e | 2005-08-31 22:21:15 +0000 | [diff] [blame] | 2954 | /* overallocate, |
Jeremy Hylton | 1aad9c7 | 2003-09-16 03:10:59 +0000 | [diff] [blame] | 2955 | to avoid reallocations */ |
Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 2956 | if (need < 2 * outlen) { |
| 2957 | if (outlen > PY_SSIZE_T_MAX / 2) { |
| 2958 | Py_DECREF(item); |
| 2959 | return NULL; |
| 2960 | } else { |
| 2961 | need = 2 * outlen; |
| 2962 | } |
| 2963 | } |
| 2964 | |
Jeremy Hylton | 364f6be | 2003-09-16 03:17:16 +0000 | [diff] [blame] | 2965 | if (PyUnicode_Resize( |
| 2966 | &result, need) < 0) { |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2967 | Py_DECREF(item); |
Walter Dörwald | 531e000 | 2003-02-04 16:57:49 +0000 | [diff] [blame] | 2968 | goto Fail_1; |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2969 | } |
| 2970 | outlen = need; |
| 2971 | } |
Jeremy Hylton | 1aad9c7 | 2003-09-16 03:10:59 +0000 | [diff] [blame] | 2972 | memcpy(PyUnicode_AS_UNICODE(result) + j, |
| 2973 | PyUnicode_AS_UNICODE(item), |
| 2974 | reslen*sizeof(Py_UNICODE)); |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2975 | j += reslen; |
| 2976 | } |
| 2977 | } |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 2978 | Py_DECREF(item); |
| 2979 | } |
| 2980 | |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2981 | if (j < outlen) |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 2982 | PyUnicode_Resize(&result, j); |
| 2983 | |
| 2984 | return result; |
| 2985 | |
| 2986 | Fail_1: |
| 2987 | Py_DECREF(result); |
| 2988 | return NULL; |
| 2989 | } |
| 2990 | #endif |