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