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