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