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