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