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