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