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