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