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