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