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