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