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