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