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