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