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