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