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