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