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