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