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