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