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