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