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