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