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