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