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" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4 | |
| 5 | #include "node.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6 | #include "code.h" |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 7 | #include "eval.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 9 | #include <ctype.h> |
| 10 | |
Mark Hammond | 26cffde4 | 2001-05-14 12:17:34 +0000 | [diff] [blame] | 11 | /* The default encoding used by the platform file system APIs |
| 12 | 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] | 13 | |
| 14 | Don't forget to modify PyUnicode_DecodeFSDefault() if you touch any of the |
| 15 | values for Py_FileSystemDefaultEncoding! |
Mark Hammond | 26cffde4 | 2001-05-14 12:17:34 +0000 | [diff] [blame] | 16 | */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 17 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Mark Hammond | 26cffde4 | 2001-05-14 12:17:34 +0000 | [diff] [blame] | 18 | const char *Py_FileSystemDefaultEncoding = "mbcs"; |
Christian Heimes | c896700 | 2007-11-30 10:18:26 +0000 | [diff] [blame] | 19 | const int Py_HasFileSystemDefaultEncoding = 1; |
Just van Rossum | b9b8e9c | 2003-02-10 09:22:01 +0000 | [diff] [blame] | 20 | #elif defined(__APPLE__) |
| 21 | const char *Py_FileSystemDefaultEncoding = "utf-8"; |
Christian Heimes | c896700 | 2007-11-30 10:18:26 +0000 | [diff] [blame] | 22 | const int Py_HasFileSystemDefaultEncoding = 1; |
Mark Hammond | 26cffde4 | 2001-05-14 12:17:34 +0000 | [diff] [blame] | 23 | #else |
| 24 | const char *Py_FileSystemDefaultEncoding = NULL; /* use default */ |
Christian Heimes | c896700 | 2007-11-30 10:18:26 +0000 | [diff] [blame] | 25 | const int Py_HasFileSystemDefaultEncoding = 0; |
Mark Hammond | 26cffde4 | 2001-05-14 12:17:34 +0000 | [diff] [blame] | 26 | #endif |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 27 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 28 | static PyObject * |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 29 | builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds) |
| 30 | { |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 31 | PyObject *func, *name, *bases, *mkw, *meta, *prep, *ns, *cell; |
| 32 | PyObject *cls = NULL; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 33 | Py_ssize_t nargs, nbases; |
| 34 | |
| 35 | assert(args != NULL); |
| 36 | if (!PyTuple_Check(args)) { |
| 37 | PyErr_SetString(PyExc_TypeError, |
| 38 | "__build_class__: args is not a tuple"); |
| 39 | return NULL; |
| 40 | } |
| 41 | nargs = PyTuple_GET_SIZE(args); |
| 42 | if (nargs < 2) { |
| 43 | PyErr_SetString(PyExc_TypeError, |
| 44 | "__build_class__: not enough arguments"); |
| 45 | return NULL; |
| 46 | } |
| 47 | func = PyTuple_GET_ITEM(args, 0); /* Better be callable */ |
| 48 | name = PyTuple_GET_ITEM(args, 1); |
Neal Norwitz | 6ea45d3 | 2007-08-26 04:19:43 +0000 | [diff] [blame] | 49 | if (!PyUnicode_Check(name)) { |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 50 | PyErr_SetString(PyExc_TypeError, |
| 51 | "__build_class__: name is not a string"); |
| 52 | return NULL; |
| 53 | } |
| 54 | bases = PyTuple_GetSlice(args, 2, nargs); |
| 55 | if (bases == NULL) |
| 56 | return NULL; |
| 57 | nbases = nargs - 2; |
| 58 | |
| 59 | if (kwds == NULL) { |
| 60 | meta = NULL; |
| 61 | mkw = NULL; |
| 62 | } |
| 63 | else { |
| 64 | mkw = PyDict_Copy(kwds); /* Don't modify kwds passed in! */ |
| 65 | if (mkw == NULL) { |
| 66 | Py_DECREF(bases); |
| 67 | return NULL; |
| 68 | } |
| 69 | meta = PyDict_GetItemString(mkw, "metaclass"); |
| 70 | if (meta != NULL) { |
| 71 | Py_INCREF(meta); |
| 72 | if (PyDict_DelItemString(mkw, "metaclass") < 0) { |
| 73 | Py_DECREF(meta); |
| 74 | Py_DECREF(mkw); |
| 75 | Py_DECREF(bases); |
| 76 | return NULL; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | if (meta == NULL) { |
| 81 | if (PyTuple_GET_SIZE(bases) == 0) |
| 82 | meta = (PyObject *) (&PyType_Type); |
| 83 | else { |
| 84 | PyObject *base0 = PyTuple_GET_ITEM(bases, 0); |
| 85 | meta = (PyObject *) (base0->ob_type); |
| 86 | } |
| 87 | Py_INCREF(meta); |
| 88 | } |
| 89 | prep = PyObject_GetAttrString(meta, "__prepare__"); |
| 90 | if (prep == NULL) { |
| 91 | PyErr_Clear(); |
| 92 | ns = PyDict_New(); |
| 93 | } |
| 94 | else { |
| 95 | PyObject *pargs = Py_BuildValue("OO", name, bases); |
| 96 | if (pargs == NULL) { |
| 97 | Py_DECREF(prep); |
| 98 | Py_DECREF(meta); |
| 99 | Py_XDECREF(mkw); |
| 100 | Py_DECREF(bases); |
| 101 | return NULL; |
| 102 | } |
| 103 | ns = PyEval_CallObjectWithKeywords(prep, pargs, mkw); |
| 104 | Py_DECREF(pargs); |
| 105 | Py_DECREF(prep); |
| 106 | if (ns == NULL) { |
| 107 | Py_DECREF(meta); |
| 108 | Py_XDECREF(mkw); |
| 109 | Py_DECREF(bases); |
| 110 | return NULL; |
| 111 | } |
| 112 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 113 | cell = PyObject_CallFunctionObjArgs(func, ns, NULL); |
| 114 | if (cell != NULL) { |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 115 | PyObject *margs; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 116 | margs = Py_BuildValue("OOO", name, bases, ns); |
| 117 | if (margs != NULL) { |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 118 | cls = PyEval_CallObjectWithKeywords(meta, margs, mkw); |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 119 | Py_DECREF(margs); |
| 120 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 121 | if (cls != NULL && PyCell_Check(cell)) { |
| 122 | Py_INCREF(cls); |
| 123 | PyCell_SET(cell, cls); |
| 124 | } |
| 125 | Py_DECREF(cell); |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 126 | } |
| 127 | Py_DECREF(ns); |
| 128 | Py_DECREF(meta); |
| 129 | Py_XDECREF(mkw); |
| 130 | Py_DECREF(bases); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 131 | return cls; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | PyDoc_STRVAR(build_class_doc, |
| 135 | "__build_class__(func, name, *bases, metaclass=None, **kwds) -> class\n\ |
| 136 | \n\ |
| 137 | Internal helper function used by the class statement."); |
| 138 | |
| 139 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 140 | builtin___import__(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 141 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 142 | static char *kwlist[] = {"name", "globals", "locals", "fromlist", |
| 143 | "level", 0}; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 144 | char *name; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 145 | PyObject *globals = NULL; |
| 146 | PyObject *locals = NULL; |
| 147 | PyObject *fromlist = NULL; |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 148 | int level = -1; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 149 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 150 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOi:__import__", |
| 151 | kwlist, &name, &globals, &locals, &fromlist, &level)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 152 | return NULL; |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 153 | return PyImport_ImportModuleLevel(name, globals, locals, |
| 154 | fromlist, level); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 157 | PyDoc_STRVAR(import_doc, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 158 | "__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 159 | \n\ |
| 160 | Import a module. The globals are only used to determine the context;\n\ |
| 161 | they are not modified. The locals are currently unused. The fromlist\n\ |
| 162 | should be a list of names to emulate ``from name import ...'', or an\n\ |
| 163 | empty list to emulate ``import name''.\n\ |
| 164 | When importing a module from a package, note that __import__('A.B', ...)\n\ |
| 165 | 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] | 166 | fromlist is not empty. Level is used to determine whether to perform \n\ |
| 167 | absolute or relative imports. -1 is the original strategy of attempting\n\ |
| 168 | both absolute and relative imports, 0 is absolute, a positive number\n\ |
| 169 | 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] | 170 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 171 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 172 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 173 | builtin_abs(PyObject *self, PyObject *v) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 174 | { |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 175 | return PyNumber_Absolute(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 178 | PyDoc_STRVAR(abs_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 179 | "abs(number) -> number\n\ |
| 180 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 181 | Return the absolute value of the argument."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 182 | |
Raymond Hettinger | 96229b1 | 2005-03-11 06:49:40 +0000 | [diff] [blame] | 183 | static PyObject * |
| 184 | builtin_all(PyObject *self, PyObject *v) |
| 185 | { |
| 186 | PyObject *it, *item; |
| 187 | |
| 188 | it = PyObject_GetIter(v); |
| 189 | if (it == NULL) |
| 190 | return NULL; |
| 191 | |
| 192 | while ((item = PyIter_Next(it)) != NULL) { |
| 193 | int cmp = PyObject_IsTrue(item); |
| 194 | Py_DECREF(item); |
| 195 | if (cmp < 0) { |
| 196 | Py_DECREF(it); |
| 197 | return NULL; |
| 198 | } |
| 199 | if (cmp == 0) { |
| 200 | Py_DECREF(it); |
| 201 | Py_RETURN_FALSE; |
| 202 | } |
| 203 | } |
| 204 | Py_DECREF(it); |
| 205 | if (PyErr_Occurred()) |
| 206 | return NULL; |
| 207 | Py_RETURN_TRUE; |
| 208 | } |
| 209 | |
| 210 | PyDoc_STRVAR(all_doc, |
| 211 | "all(iterable) -> bool\n\ |
| 212 | \n\ |
| 213 | Return True if bool(x) is True for all values x in the iterable."); |
| 214 | |
| 215 | static PyObject * |
| 216 | builtin_any(PyObject *self, PyObject *v) |
| 217 | { |
| 218 | PyObject *it, *item; |
| 219 | |
| 220 | it = PyObject_GetIter(v); |
| 221 | if (it == NULL) |
| 222 | return NULL; |
| 223 | |
| 224 | while ((item = PyIter_Next(it)) != NULL) { |
| 225 | int cmp = PyObject_IsTrue(item); |
| 226 | Py_DECREF(item); |
| 227 | if (cmp < 0) { |
| 228 | Py_DECREF(it); |
| 229 | return NULL; |
| 230 | } |
| 231 | if (cmp == 1) { |
| 232 | Py_DECREF(it); |
| 233 | Py_RETURN_TRUE; |
| 234 | } |
| 235 | } |
| 236 | Py_DECREF(it); |
| 237 | if (PyErr_Occurred()) |
| 238 | return NULL; |
| 239 | Py_RETURN_FALSE; |
| 240 | } |
| 241 | |
| 242 | PyDoc_STRVAR(any_doc, |
| 243 | "any(iterable) -> bool\n\ |
| 244 | \n\ |
| 245 | 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] | 246 | |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 247 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 248 | static PyObject * |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 249 | builtin_bin(PyObject *self, PyObject *v) |
| 250 | { |
| 251 | return PyNumber_ToBase(v, 2); |
| 252 | } |
| 253 | |
| 254 | PyDoc_STRVAR(bin_doc, |
| 255 | "bin(number) -> string\n\ |
| 256 | \n\ |
| 257 | Return the binary representation of an integer or long integer."); |
| 258 | |
| 259 | |
| 260 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 261 | builtin_filter(PyObject *self, PyObject *args) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 262 | { |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 263 | PyObject *itertools, *ifilter, *result; |
| 264 | itertools = PyImport_ImportModule("itertools"); |
| 265 | if (itertools == NULL) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 266 | return NULL; |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 267 | ifilter = PyObject_GetAttrString(itertools, "ifilter"); |
| 268 | Py_DECREF(itertools); |
| 269 | if (ifilter == NULL) |
Georg Brandl | e35b657 | 2005-07-19 22:20:20 +0000 | [diff] [blame] | 270 | return NULL; |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 271 | result = PyObject_Call(ifilter, args, NULL); |
| 272 | Py_DECREF(ifilter); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 273 | return result; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 276 | PyDoc_STRVAR(filter_doc, |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 277 | "filter(predicate, iterable) -> iterator\n\ |
| 278 | \n\ |
| 279 | Return an iterator yielding only those elements of the input iterable\n\ |
| 280 | for which the predicate (a Boolean function) returns true.\n\ |
| 281 | If the predicate is None, 'lambda x: bool(x)' is assumed.\n\ |
| 282 | (This is identical to itertools.ifilter().)"); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 283 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 284 | static PyObject * |
| 285 | builtin_format(PyObject *self, PyObject *args) |
| 286 | { |
Christian Heimes | 94b7d3d | 2007-12-11 20:20:39 +0000 | [diff] [blame] | 287 | static PyObject * format_str = NULL; |
| 288 | PyObject *value; |
| 289 | PyObject *spec = NULL; |
| 290 | PyObject *meth; |
| 291 | PyObject *empty = NULL; |
| 292 | PyObject *result = NULL; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 293 | |
Christian Heimes | 94b7d3d | 2007-12-11 20:20:39 +0000 | [diff] [blame] | 294 | /* Initialize cached value */ |
| 295 | if (format_str == NULL) { |
| 296 | /* Initialize static variable needed by _PyType_Lookup */ |
| 297 | format_str = PyUnicode_FromString("__format__"); |
| 298 | if (format_str == NULL) |
| 299 | goto done; |
| 300 | } |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 301 | |
Christian Heimes | 94b7d3d | 2007-12-11 20:20:39 +0000 | [diff] [blame] | 302 | if (!PyArg_ParseTuple(args, "O|U:format", &value, &spec)) |
| 303 | goto done; |
Eric Smith | 8193669 | 2007-08-31 01:14:01 +0000 | [diff] [blame] | 304 | |
Christian Heimes | 94b7d3d | 2007-12-11 20:20:39 +0000 | [diff] [blame] | 305 | /* initialize the default value */ |
| 306 | if (spec == NULL) { |
| 307 | empty = PyUnicode_FromUnicode(NULL, 0); |
| 308 | spec = empty; |
| 309 | } |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 310 | |
Christian Heimes | 94b7d3d | 2007-12-11 20:20:39 +0000 | [diff] [blame] | 311 | /* Make sure the type is initialized. float gets initialized late */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame^] | 312 | if (Py_TYPE(value)->tp_dict == NULL) |
| 313 | if (PyType_Ready(Py_TYPE(value)) < 0) |
Christian Heimes | 94b7d3d | 2007-12-11 20:20:39 +0000 | [diff] [blame] | 314 | goto done; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 315 | |
Christian Heimes | 94b7d3d | 2007-12-11 20:20:39 +0000 | [diff] [blame] | 316 | /* Find the (unbound!) __format__ method (a borrowed reference) */ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame^] | 317 | meth = _PyType_Lookup(Py_TYPE(value), format_str); |
Christian Heimes | 94b7d3d | 2007-12-11 20:20:39 +0000 | [diff] [blame] | 318 | if (meth == NULL) { |
| 319 | PyErr_Format(PyExc_TypeError, |
| 320 | "Type %.100s doesn't define __format__", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame^] | 321 | Py_TYPE(value)->tp_name); |
Christian Heimes | 94b7d3d | 2007-12-11 20:20:39 +0000 | [diff] [blame] | 322 | goto done; |
| 323 | } |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 324 | |
Christian Heimes | 94b7d3d | 2007-12-11 20:20:39 +0000 | [diff] [blame] | 325 | /* And call it, binding it to the value */ |
| 326 | result = PyObject_CallFunctionObjArgs(meth, value, spec, NULL); |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 327 | |
Christian Heimes | 94b7d3d | 2007-12-11 20:20:39 +0000 | [diff] [blame] | 328 | if (result && !PyUnicode_Check(result)) { |
| 329 | PyErr_SetString(PyExc_TypeError, |
| 330 | "__format__ method did not return string"); |
| 331 | Py_DECREF(result); |
| 332 | result = NULL; |
| 333 | goto done; |
| 334 | } |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 335 | |
Eric Smith | 8193669 | 2007-08-31 01:14:01 +0000 | [diff] [blame] | 336 | done: |
Christian Heimes | 94b7d3d | 2007-12-11 20:20:39 +0000 | [diff] [blame] | 337 | Py_XDECREF(empty); |
| 338 | return result; |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 341 | PyDoc_STRVAR(format_doc, |
Eric Smith | 8193669 | 2007-08-31 01:14:01 +0000 | [diff] [blame] | 342 | "format(value[, format_spec]) -> string\n\ |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 343 | \n\ |
Eric Smith | 8193669 | 2007-08-31 01:14:01 +0000 | [diff] [blame] | 344 | Returns value.__format__(format_spec)\n\ |
| 345 | format_spec defaults to \"\""); |
| 346 | |
Guido van Rossum | 7fcf224 | 2007-05-04 17:43:11 +0000 | [diff] [blame] | 347 | static PyObject * |
Walter Dörwald | e7efd59 | 2007-06-05 20:07:21 +0000 | [diff] [blame] | 348 | builtin_chr(PyObject *self, PyObject *args) |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 349 | { |
| 350 | long x; |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 351 | |
Walter Dörwald | e7efd59 | 2007-06-05 20:07:21 +0000 | [diff] [blame] | 352 | if (!PyArg_ParseTuple(args, "l:chr", &x)) |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 353 | return NULL; |
Fredrik Lundh | 0dcf67e | 2001-06-26 20:01:56 +0000 | [diff] [blame] | 354 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 355 | return PyUnicode_FromOrdinal(x); |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Guido van Rossum | 307fa8c | 2007-07-16 20:46:27 +0000 | [diff] [blame] | 358 | PyDoc_VAR(chr_doc) = PyDoc_STR( |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 359 | "chr(i) -> Unicode character\n\ |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 360 | \n\ |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 361 | 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] | 362 | ) |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 363 | #ifndef Py_UNICODE_WIDE |
Guido van Rossum | 307fa8c | 2007-07-16 20:46:27 +0000 | [diff] [blame] | 364 | PyDoc_STR( |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 365 | "\nIf 0x10000 <= i, a surrogate pair is returned." |
Guido van Rossum | 307fa8c | 2007-07-16 20:46:27 +0000 | [diff] [blame] | 366 | ) |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 367 | #endif |
Guido van Rossum | 307fa8c | 2007-07-16 20:46:27 +0000 | [diff] [blame] | 368 | ; |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 369 | |
| 370 | |
| 371 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 372 | builtin_cmp(PyObject *self, PyObject *args) |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 373 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 374 | PyObject *a, *b; |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 375 | int c; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 376 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 377 | if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b)) |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 378 | return NULL; |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 379 | if (PyObject_Cmp(a, b, &c) < 0) |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 380 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 381 | return PyLong_FromLong((long)c); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 384 | PyDoc_STRVAR(cmp_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 385 | "cmp(x, y) -> integer\n\ |
| 386 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 387 | Return negative if x<y, zero if x==y, positive if x>y."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 388 | |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 389 | |
| 390 | static char * |
| 391 | source_as_string(PyObject *cmd) |
| 392 | { |
| 393 | char *str; |
| 394 | Py_ssize_t size; |
| 395 | |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 396 | if (PyUnicode_Check(cmd)) { |
| 397 | cmd = _PyUnicode_AsDefaultEncodedString(cmd, NULL); |
| 398 | if (cmd == NULL) |
| 399 | return NULL; |
| 400 | } |
Guido van Rossum | a4b8d1d | 2007-08-27 15:02:28 +0000 | [diff] [blame] | 401 | else if (!PyObject_CheckReadBuffer(cmd)) { |
| 402 | PyErr_SetString(PyExc_TypeError, |
| 403 | "eval()/exec() arg 1 must be a string, bytes or code object"); |
| 404 | return NULL; |
| 405 | } |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 406 | if (PyObject_AsReadBuffer(cmd, (const void **)&str, &size) < 0) { |
| 407 | return NULL; |
| 408 | } |
| 409 | if (strlen(str) != size) { |
| 410 | PyErr_SetString(PyExc_TypeError, |
| 411 | "source code string cannot contain null bytes"); |
| 412 | return NULL; |
| 413 | } |
| 414 | return str; |
| 415 | } |
| 416 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 417 | static PyObject * |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 418 | builtin_compile(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 419 | { |
| 420 | char *str; |
| 421 | char *filename; |
| 422 | char *startstr; |
| 423 | int start; |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 424 | int dont_inherit = 0; |
| 425 | int supplied_flags = 0; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 426 | PyCompilerFlags cf; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 427 | PyObject *cmd; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 428 | static char *kwlist[] = {"source", "filename", "mode", "flags", |
| 429 | "dont_inherit", NULL}; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 430 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 431 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile", |
| 432 | kwlist, &cmd, &filename, &startstr, |
| 433 | &supplied_flags, &dont_inherit)) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 434 | return NULL; |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 435 | |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 436 | cf.cf_flags = supplied_flags | PyCF_SOURCE_IS_UTF8; |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 437 | |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 438 | str = source_as_string(cmd); |
| 439 | if (str == NULL) |
Just van Rossum | b9b8e9c | 2003-02-10 09:22:01 +0000 | [diff] [blame] | 440 | return NULL; |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 441 | |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 442 | if (strcmp(startstr, "exec") == 0) |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 443 | start = Py_file_input; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 444 | else if (strcmp(startstr, "eval") == 0) |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 445 | start = Py_eval_input; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 446 | else if (strcmp(startstr, "single") == 0) |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 447 | start = Py_single_input; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 448 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 449 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 450 | "compile() arg 3 must be 'exec' or 'eval' or 'single'"); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 451 | return NULL; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 452 | } |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 453 | |
Guido van Rossum | 4b499dd3 | 2003-02-13 22:07:59 +0000 | [diff] [blame] | 454 | if (supplied_flags & |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 455 | ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST)) |
Guido van Rossum | 4b499dd3 | 2003-02-13 22:07:59 +0000 | [diff] [blame] | 456 | { |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 457 | PyErr_SetString(PyExc_ValueError, |
| 458 | "compile(): unrecognised flags"); |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 459 | return NULL; |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 460 | } |
| 461 | /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */ |
| 462 | |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 463 | if (!dont_inherit) { |
| 464 | PyEval_MergeCompilerFlags(&cf); |
| 465 | } |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 466 | return Py_CompileStringFlags(str, filename, start, &cf); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 469 | PyDoc_STRVAR(compile_doc, |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 470 | "compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 471 | \n\ |
| 472 | Compile the source string (a Python module, statement or expression)\n\ |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 473 | 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] | 474 | The filename will be used for run-time error messages.\n\ |
| 475 | 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] | 476 | single (interactive) statement, or 'eval' to compile an expression.\n\ |
| 477 | The flags argument, if present, controls which future statements influence\n\ |
| 478 | the compilation of the code.\n\ |
| 479 | The dont_inherit argument, if non-zero, stops the compilation inheriting\n\ |
| 480 | the effects of any future statements in effect in the code calling\n\ |
| 481 | 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] | 482 | in addition to any features explicitly specified."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 483 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 484 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 485 | builtin_dir(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 486 | { |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 487 | PyObject *arg = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 488 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 489 | if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 490 | return NULL; |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 491 | return PyObject_Dir(arg); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 494 | PyDoc_STRVAR(dir_doc, |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 495 | "dir([object]) -> list of strings\n" |
| 496 | "\n" |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 497 | "If called without an argument, return the names in the current scope.\n" |
| 498 | "Else, return an alphabetized list of names comprising (some of) the attributes\n" |
| 499 | "of the given object, and of attributes reachable from it.\n" |
| 500 | "If the object supplies a method named __dir__, it will be used; otherwise\n" |
| 501 | "the default dir() logic is used and returns:\n" |
| 502 | " for a module object: the module's attributes.\n" |
| 503 | " for a class object: its attributes, and recursively the attributes\n" |
| 504 | " of its bases.\n" |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 505 | " for any other object: its attributes, its class's attributes, and\n" |
Georg Brandl | e32b422 | 2007-03-10 22:13:27 +0000 | [diff] [blame] | 506 | " recursively the attributes of its class's base classes."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 507 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 508 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 509 | builtin_divmod(PyObject *self, PyObject *args) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 510 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 511 | PyObject *v, *w; |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 512 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 513 | if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w)) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 514 | return NULL; |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 515 | return PyNumber_Divmod(v, w); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 518 | PyDoc_STRVAR(divmod_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 519 | "divmod(x, y) -> (div, mod)\n\ |
| 520 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 521 | 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] | 522 | |
| 523 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 524 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 525 | builtin_eval(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 526 | { |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 527 | PyObject *cmd, *result, *tmp = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 528 | PyObject *globals = Py_None, *locals = Py_None; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 529 | char *str; |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 530 | PyCompilerFlags cf; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 531 | |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 532 | if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 533 | return NULL; |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 534 | if (locals != Py_None && !PyMapping_Check(locals)) { |
| 535 | PyErr_SetString(PyExc_TypeError, "locals must be a mapping"); |
Raymond Hettinger | 513ffe8 | 2004-07-06 13:44:41 +0000 | [diff] [blame] | 536 | return NULL; |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 537 | } |
| 538 | if (globals != Py_None && !PyDict_Check(globals)) { |
Georg Brandl | 99d7e4e | 2005-08-31 22:21:15 +0000 | [diff] [blame] | 539 | PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ? |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 540 | "globals must be a real dict; try eval(expr, {}, mapping)" |
| 541 | : "globals must be a dict"); |
Raymond Hettinger | 513ffe8 | 2004-07-06 13:44:41 +0000 | [diff] [blame] | 542 | return NULL; |
Raymond Hettinger | 214b1c3 | 2004-07-02 06:41:07 +0000 | [diff] [blame] | 543 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 544 | if (globals == Py_None) { |
| 545 | globals = PyEval_GetGlobals(); |
| 546 | if (locals == Py_None) |
| 547 | locals = PyEval_GetLocals(); |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 548 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 549 | else if (locals == Py_None) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 550 | locals = globals; |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 551 | |
Georg Brandl | 77c85e6 | 2005-09-15 10:46:13 +0000 | [diff] [blame] | 552 | if (globals == NULL || locals == NULL) { |
| 553 | PyErr_SetString(PyExc_TypeError, |
| 554 | "eval must be given globals and locals " |
| 555 | "when called without a frame"); |
| 556 | return NULL; |
| 557 | } |
| 558 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 559 | if (PyDict_GetItemString(globals, "__builtins__") == NULL) { |
| 560 | if (PyDict_SetItemString(globals, "__builtins__", |
| 561 | PyEval_GetBuiltins()) != 0) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 562 | return NULL; |
| 563 | } |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 564 | |
Jeremy Hylton | 15c1c4f | 2001-07-30 21:50:55 +0000 | [diff] [blame] | 565 | if (PyCode_Check(cmd)) { |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 566 | if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) { |
Jeremy Hylton | 15c1c4f | 2001-07-30 21:50:55 +0000 | [diff] [blame] | 567 | PyErr_SetString(PyExc_TypeError, |
| 568 | "code object passed to eval() may not contain free variables"); |
| 569 | return NULL; |
| 570 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 571 | return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals); |
Jeremy Hylton | 15c1c4f | 2001-07-30 21:50:55 +0000 | [diff] [blame] | 572 | } |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 573 | |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 574 | str = source_as_string(cmd); |
| 575 | if (str == NULL) |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 576 | return NULL; |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 577 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 578 | while (*str == ' ' || *str == '\t') |
| 579 | str++; |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 580 | |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 581 | cf.cf_flags = PyCF_SOURCE_IS_UTF8; |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 582 | (void)PyEval_MergeCompilerFlags(&cf); |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 583 | result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf); |
| 584 | Py_XDECREF(tmp); |
| 585 | return result; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 588 | PyDoc_STRVAR(eval_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 589 | "eval(source[, globals[, locals]]) -> value\n\ |
| 590 | \n\ |
| 591 | Evaluate the source in the context of globals and locals.\n\ |
| 592 | The source may be a string representing a Python expression\n\ |
| 593 | or a code object as returned by compile().\n\ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 594 | 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] | 595 | defaulting to the current globals and locals.\n\ |
| 596 | If only globals is given, locals defaults to it.\n"); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 597 | |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 598 | static PyObject * |
| 599 | builtin_exec(PyObject *self, PyObject *args) |
| 600 | { |
| 601 | PyObject *v; |
| 602 | PyObject *prog, *globals = Py_None, *locals = Py_None; |
| 603 | int plain = 0; |
| 604 | |
| 605 | if (!PyArg_ParseTuple(args, "O|OO:exec", &prog, &globals, &locals)) |
| 606 | return NULL; |
| 607 | |
| 608 | if (globals == Py_None) { |
| 609 | globals = PyEval_GetGlobals(); |
| 610 | if (locals == Py_None) { |
| 611 | locals = PyEval_GetLocals(); |
| 612 | plain = 1; |
| 613 | } |
| 614 | if (!globals || !locals) { |
| 615 | PyErr_SetString(PyExc_SystemError, |
| 616 | "globals and locals cannot be NULL"); |
| 617 | return NULL; |
| 618 | } |
| 619 | } |
| 620 | else if (locals == Py_None) |
| 621 | locals = globals; |
Neal Norwitz | 6ea45d3 | 2007-08-26 04:19:43 +0000 | [diff] [blame] | 622 | if (!PyUnicode_Check(prog) && |
Guido van Rossum | da5b8f2 | 2007-06-12 23:30:11 +0000 | [diff] [blame] | 623 | !PyCode_Check(prog)) { |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 624 | PyErr_Format(PyExc_TypeError, |
| 625 | "exec() arg 1 must be a string, file, or code " |
| 626 | "object, not %.100s", prog->ob_type->tp_name); |
| 627 | return NULL; |
| 628 | } |
| 629 | if (!PyDict_Check(globals)) { |
| 630 | PyErr_Format(PyExc_TypeError, "exec() arg 2 must be a dict, not %.100s", |
| 631 | globals->ob_type->tp_name); |
| 632 | return NULL; |
| 633 | } |
| 634 | if (!PyMapping_Check(locals)) { |
| 635 | PyErr_Format(PyExc_TypeError, |
| 636 | "arg 3 must be a mapping or None, not %.100s", |
| 637 | locals->ob_type->tp_name); |
| 638 | return NULL; |
| 639 | } |
| 640 | if (PyDict_GetItemString(globals, "__builtins__") == NULL) { |
| 641 | if (PyDict_SetItemString(globals, "__builtins__", |
| 642 | PyEval_GetBuiltins()) != 0) |
| 643 | return NULL; |
| 644 | } |
| 645 | |
| 646 | if (PyCode_Check(prog)) { |
| 647 | if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) { |
| 648 | PyErr_SetString(PyExc_TypeError, |
| 649 | "code object passed to exec() may not " |
| 650 | "contain free variables"); |
| 651 | return NULL; |
| 652 | } |
| 653 | v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals); |
| 654 | } |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 655 | else { |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 656 | char *str = source_as_string(prog); |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 657 | PyCompilerFlags cf; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 658 | if (str == NULL) |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 659 | return NULL; |
Guido van Rossum | f15a29f | 2007-05-04 00:41:39 +0000 | [diff] [blame] | 660 | cf.cf_flags = PyCF_SOURCE_IS_UTF8; |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 661 | if (PyEval_MergeCompilerFlags(&cf)) |
| 662 | v = PyRun_StringFlags(str, Py_file_input, globals, |
| 663 | locals, &cf); |
| 664 | else |
| 665 | v = PyRun_String(str, Py_file_input, globals, locals); |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 666 | } |
| 667 | if (v == NULL) |
| 668 | return NULL; |
| 669 | Py_DECREF(v); |
| 670 | Py_RETURN_NONE; |
| 671 | } |
| 672 | |
| 673 | PyDoc_STRVAR(exec_doc, |
| 674 | "exec(object[, globals[, locals]])\n\ |
| 675 | \n\ |
| 676 | Read and execute code from a object, which can be a string, a code\n\ |
| 677 | object or a file object.\n\ |
| 678 | The globals and locals are dictionaries, defaulting to the current\n\ |
| 679 | globals and locals. If only globals is given, locals defaults to it."); |
| 680 | |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 681 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 682 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 683 | builtin_getattr(PyObject *self, PyObject *args) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 684 | { |
Neal Norwitz | 6ea45d3 | 2007-08-26 04:19:43 +0000 | [diff] [blame] | 685 | PyObject *v, *result, *dflt = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 686 | PyObject *name; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 687 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 688 | if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt)) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 689 | return NULL; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 690 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 691 | if (!PyUnicode_Check(name)) { |
Jeremy Hylton | 0eb1115 | 2001-07-30 22:39:31 +0000 | [diff] [blame] | 692 | PyErr_SetString(PyExc_TypeError, |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 693 | "getattr(): attribute name must be string"); |
Jeremy Hylton | 0eb1115 | 2001-07-30 22:39:31 +0000 | [diff] [blame] | 694 | return NULL; |
| 695 | } |
Guido van Rossum | 950ff29 | 1998-06-29 13:38:57 +0000 | [diff] [blame] | 696 | result = PyObject_GetAttr(v, name); |
Guido van Rossum | d892357 | 2001-10-16 21:31:32 +0000 | [diff] [blame] | 697 | if (result == NULL && dflt != NULL && |
| 698 | PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 699 | { |
Guido van Rossum | 950ff29 | 1998-06-29 13:38:57 +0000 | [diff] [blame] | 700 | PyErr_Clear(); |
| 701 | Py_INCREF(dflt); |
| 702 | result = dflt; |
| 703 | } |
| 704 | return result; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 707 | PyDoc_STRVAR(getattr_doc, |
Guido van Rossum | 950ff29 | 1998-06-29 13:38:57 +0000 | [diff] [blame] | 708 | "getattr(object, name[, default]) -> value\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 709 | \n\ |
Guido van Rossum | 950ff29 | 1998-06-29 13:38:57 +0000 | [diff] [blame] | 710 | Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\ |
| 711 | 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] | 712 | exist; without it, an exception is raised in that case."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 713 | |
| 714 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 715 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 716 | builtin_globals(PyObject *self) |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 717 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 718 | PyObject *d; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 719 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 720 | d = PyEval_GetGlobals(); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 721 | Py_XINCREF(d); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 722 | return d; |
| 723 | } |
| 724 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 725 | PyDoc_STRVAR(globals_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 726 | "globals() -> dictionary\n\ |
| 727 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 728 | Return the dictionary containing the current scope's global variables."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 729 | |
| 730 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 731 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 732 | builtin_hasattr(PyObject *self, PyObject *args) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 733 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 734 | PyObject *v; |
| 735 | PyObject *name; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 736 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 737 | if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name)) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 738 | return NULL; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 739 | if (!PyUnicode_Check(name)) { |
Jeremy Hylton | 302b54a | 2001-07-30 22:45:19 +0000 | [diff] [blame] | 740 | PyErr_SetString(PyExc_TypeError, |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 741 | "hasattr(): attribute name must be string"); |
Jeremy Hylton | 302b54a | 2001-07-30 22:45:19 +0000 | [diff] [blame] | 742 | return NULL; |
| 743 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 744 | v = PyObject_GetAttr(v, name); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 745 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 746 | PyErr_Clear(); |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 747 | Py_INCREF(Py_False); |
| 748 | return Py_False; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 749 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 750 | Py_DECREF(v); |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 751 | Py_INCREF(Py_True); |
| 752 | return Py_True; |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 755 | PyDoc_STRVAR(hasattr_doc, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 756 | "hasattr(object, name) -> bool\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 757 | \n\ |
| 758 | Return whether the object has an attribute with the given name.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 759 | (This is done by calling getattr(object, name) and catching exceptions.)"); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 760 | |
| 761 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 762 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 763 | builtin_id(PyObject *self, PyObject *v) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 764 | { |
Guido van Rossum | 106f2da | 2000-06-28 21:12:25 +0000 | [diff] [blame] | 765 | return PyLong_FromVoidPtr(v); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 768 | PyDoc_STRVAR(id_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 769 | "id(object) -> integer\n\ |
| 770 | \n\ |
| 771 | 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] | 772 | simultaneously existing objects. (Hint: it's the object's memory address.)"); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 773 | |
| 774 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 775 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 776 | builtin_map(PyObject *self, PyObject *args) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 777 | { |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 778 | PyObject *itertools, *imap, *result; |
| 779 | itertools = PyImport_ImportModule("itertools"); |
| 780 | if (itertools == NULL) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 781 | return NULL; |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 782 | imap = PyObject_GetAttrString(itertools, "imap"); |
| 783 | Py_DECREF(itertools); |
| 784 | if (imap == NULL) |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 785 | return NULL; |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 786 | result = PyObject_Call(imap, args, NULL); |
| 787 | Py_DECREF(imap); |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 788 | return result; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 791 | PyDoc_STRVAR(map_doc, |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 792 | "map(function, iterable[, iterable, ...]) -> iterator\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 793 | \n\ |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 794 | Return an iterator yielding the results of applying the function to the\n\ |
| 795 | items of the argument iterables(s). If more than one iterable is given,\n\ |
| 796 | the function is called with an argument list consisting of the\n\ |
| 797 | corresponding item of each iterable, until an iterable is exhausted.\n\ |
| 798 | If the function is None, 'lambda *a: a' is assumed.\n\ |
| 799 | (This is identical to itertools.imap().)"); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 800 | |
| 801 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 802 | static PyObject * |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 803 | builtin_next(PyObject *self, PyObject *args) |
| 804 | { |
| 805 | PyObject *it, *res; |
| 806 | PyObject *def = NULL; |
| 807 | |
| 808 | if (!PyArg_UnpackTuple(args, "next", 1, 2, &it, &def)) |
| 809 | return NULL; |
| 810 | if (!PyIter_Check(it)) { |
| 811 | PyErr_Format(PyExc_TypeError, |
| 812 | "%.200s object is not an iterator", it->ob_type->tp_name); |
| 813 | return NULL; |
| 814 | } |
| 815 | |
| 816 | res = (*it->ob_type->tp_iternext)(it); |
| 817 | if (res == NULL) { |
| 818 | if (def) { |
| 819 | if (PyErr_Occurred() && |
| 820 | !PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 821 | return NULL; |
| 822 | PyErr_Clear(); |
| 823 | Py_INCREF(def); |
| 824 | return def; |
| 825 | } else if (PyErr_Occurred()) { |
| 826 | return NULL; |
| 827 | } else { |
| 828 | PyErr_SetNone(PyExc_StopIteration); |
| 829 | return NULL; |
| 830 | } |
| 831 | } |
| 832 | return res; |
| 833 | } |
| 834 | |
| 835 | PyDoc_STRVAR(next_doc, |
| 836 | "next(iterator[, default])\n\ |
| 837 | \n\ |
| 838 | Return the next item from the iterator. If default is given and the iterator\n\ |
| 839 | is exhausted, it is returned instead of raising StopIteration."); |
| 840 | |
| 841 | |
| 842 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 843 | builtin_setattr(PyObject *self, PyObject *args) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 844 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 845 | PyObject *v; |
| 846 | PyObject *name; |
| 847 | PyObject *value; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 848 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 849 | if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value)) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 850 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 851 | if (PyObject_SetAttr(v, name, value) != 0) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 852 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 853 | Py_INCREF(Py_None); |
| 854 | return Py_None; |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 857 | PyDoc_STRVAR(setattr_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 858 | "setattr(object, name, value)\n\ |
| 859 | \n\ |
| 860 | 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] | 861 | ``x.y = v''."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 862 | |
| 863 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 864 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 865 | builtin_delattr(PyObject *self, PyObject *args) |
Guido van Rossum | 14144fc | 1994-08-29 12:53:40 +0000 | [diff] [blame] | 866 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 867 | PyObject *v; |
| 868 | PyObject *name; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 869 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 870 | if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name)) |
Guido van Rossum | 14144fc | 1994-08-29 12:53:40 +0000 | [diff] [blame] | 871 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 872 | if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0) |
Guido van Rossum | 14144fc | 1994-08-29 12:53:40 +0000 | [diff] [blame] | 873 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 874 | Py_INCREF(Py_None); |
| 875 | return Py_None; |
Guido van Rossum | 14144fc | 1994-08-29 12:53:40 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 878 | PyDoc_STRVAR(delattr_doc, |
Guido van Rossum | df12a59 | 1998-11-23 22:13:04 +0000 | [diff] [blame] | 879 | "delattr(object, name)\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 880 | \n\ |
| 881 | 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] | 882 | ``del x.y''."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 883 | |
| 884 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 885 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 886 | builtin_hash(PyObject *self, PyObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 887 | { |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 888 | long x; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 889 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 890 | x = PyObject_Hash(v); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 891 | if (x == -1) |
| 892 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 893 | return PyLong_FromLong(x); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 896 | PyDoc_STRVAR(hash_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 897 | "hash(object) -> integer\n\ |
| 898 | \n\ |
| 899 | 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] | 900 | 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] | 901 | |
| 902 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 903 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 904 | builtin_hex(PyObject *self, PyObject *v) |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 905 | { |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 906 | return PyNumber_ToBase(v, 16); |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 909 | PyDoc_STRVAR(hex_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 910 | "hex(number) -> string\n\ |
| 911 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 912 | Return the hexadecimal representation of an integer or long integer."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 913 | |
| 914 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 915 | static PyObject * |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 916 | builtin_iter(PyObject *self, PyObject *args) |
| 917 | { |
| 918 | PyObject *v, *w = NULL; |
| 919 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 920 | if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w)) |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 921 | return NULL; |
| 922 | if (w == NULL) |
| 923 | return PyObject_GetIter(v); |
| 924 | if (!PyCallable_Check(v)) { |
| 925 | PyErr_SetString(PyExc_TypeError, |
| 926 | "iter(v, w): v must be callable"); |
| 927 | return NULL; |
| 928 | } |
| 929 | return PyCallIter_New(v, w); |
| 930 | } |
| 931 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 932 | PyDoc_STRVAR(iter_doc, |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 933 | "iter(collection) -> iterator\n\ |
| 934 | iter(callable, sentinel) -> iterator\n\ |
| 935 | \n\ |
| 936 | Get an iterator from an object. In the first form, the argument must\n\ |
| 937 | supply its own iterator, or be a sequence.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 938 | 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] | 939 | |
| 940 | |
| 941 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 942 | builtin_len(PyObject *self, PyObject *v) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 943 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 944 | Py_ssize_t res; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 945 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 946 | res = PyObject_Size(v); |
Guido van Rossum | 8ea9f4d | 1998-06-29 22:26:50 +0000 | [diff] [blame] | 947 | if (res < 0 && PyErr_Occurred()) |
| 948 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 949 | return PyLong_FromSsize_t(res); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 950 | } |
| 951 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 952 | PyDoc_STRVAR(len_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 953 | "len(object) -> integer\n\ |
| 954 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 955 | Return the number of items of a sequence or mapping."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 956 | |
| 957 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 958 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 959 | builtin_locals(PyObject *self) |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 960 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 961 | PyObject *d; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 962 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 963 | d = PyEval_GetLocals(); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 964 | Py_XINCREF(d); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 965 | return d; |
| 966 | } |
| 967 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 968 | PyDoc_STRVAR(locals_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 969 | "locals() -> dictionary\n\ |
| 970 | \n\ |
Raymond Hettinger | 69bf8f3 | 2003-01-04 02:16:22 +0000 | [diff] [blame] | 971 | 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] | 972 | |
| 973 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 974 | static PyObject * |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 975 | min_max(PyObject *args, PyObject *kwds, int op) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 976 | { |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 977 | PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL; |
Martin v. Löwis | fd39ad4 | 2004-08-12 14:42:37 +0000 | [diff] [blame] | 978 | const char *name = op == Py_LT ? "min" : "max"; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 979 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 980 | if (PyTuple_Size(args) > 1) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 981 | v = args; |
Martin v. Löwis | fd39ad4 | 2004-08-12 14:42:37 +0000 | [diff] [blame] | 982 | else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v)) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 983 | return NULL; |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 984 | |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 985 | if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) { |
| 986 | keyfunc = PyDict_GetItemString(kwds, "key"); |
| 987 | if (PyDict_Size(kwds)!=1 || keyfunc == NULL) { |
Georg Brandl | 99d7e4e | 2005-08-31 22:21:15 +0000 | [diff] [blame] | 988 | PyErr_Format(PyExc_TypeError, |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 989 | "%s() got an unexpected keyword argument", name); |
| 990 | return NULL; |
| 991 | } |
Georg Brandl | 99d7e4e | 2005-08-31 22:21:15 +0000 | [diff] [blame] | 992 | } |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 993 | |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 994 | it = PyObject_GetIter(v); |
| 995 | if (it == NULL) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 996 | return NULL; |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 997 | |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 998 | maxitem = NULL; /* the result */ |
| 999 | maxval = NULL; /* the value associated with the result */ |
Brett Cannon | 9e635cf | 2004-12-07 00:25:35 +0000 | [diff] [blame] | 1000 | while (( item = PyIter_Next(it) )) { |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1001 | /* get the value from the key function */ |
| 1002 | if (keyfunc != NULL) { |
| 1003 | val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL); |
| 1004 | if (val == NULL) |
| 1005 | goto Fail_it_item; |
| 1006 | } |
| 1007 | /* no key function; the value is the item */ |
| 1008 | else { |
| 1009 | val = item; |
| 1010 | Py_INCREF(val); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1011 | } |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 1012 | |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1013 | /* maximum value and item are unset; set them */ |
| 1014 | if (maxval == NULL) { |
| 1015 | maxitem = item; |
| 1016 | maxval = val; |
| 1017 | } |
| 1018 | /* maximum value and item are set; update them as necessary */ |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1019 | else { |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1020 | int cmp = PyObject_RichCompareBool(val, maxval, op); |
| 1021 | if (cmp < 0) |
| 1022 | goto Fail_it_item_and_val; |
| 1023 | else if (cmp > 0) { |
| 1024 | Py_DECREF(maxval); |
| 1025 | Py_DECREF(maxitem); |
| 1026 | maxval = val; |
| 1027 | maxitem = item; |
Guido van Rossum | 53451b3 | 2001-01-17 15:47:24 +0000 | [diff] [blame] | 1028 | } |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1029 | else { |
| 1030 | Py_DECREF(item); |
| 1031 | Py_DECREF(val); |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 1032 | } |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1033 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1034 | } |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1035 | if (PyErr_Occurred()) |
| 1036 | goto Fail_it; |
| 1037 | if (maxval == NULL) { |
Martin v. Löwis | fd39ad4 | 2004-08-12 14:42:37 +0000 | [diff] [blame] | 1038 | PyErr_Format(PyExc_ValueError, |
| 1039 | "%s() arg is an empty sequence", name); |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1040 | assert(maxitem == NULL); |
| 1041 | } |
| 1042 | else |
| 1043 | Py_DECREF(maxval); |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 1044 | Py_DECREF(it); |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1045 | return maxitem; |
| 1046 | |
| 1047 | Fail_it_item_and_val: |
| 1048 | Py_DECREF(val); |
| 1049 | Fail_it_item: |
| 1050 | Py_DECREF(item); |
| 1051 | Fail_it: |
| 1052 | Py_XDECREF(maxval); |
| 1053 | Py_XDECREF(maxitem); |
| 1054 | Py_DECREF(it); |
| 1055 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1058 | static PyObject * |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1059 | builtin_min(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1060 | { |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1061 | return min_max(args, kwds, Py_LT); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1064 | PyDoc_STRVAR(min_doc, |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1065 | "min(iterable[, key=func]) -> value\n\ |
| 1066 | min(a, b, c, ...[, key=func]) -> value\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1067 | \n\ |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1068 | With a single iterable argument, return its smallest item.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1069 | With two or more arguments, return the smallest argument."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1070 | |
| 1071 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1072 | static PyObject * |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1073 | builtin_max(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1074 | { |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1075 | return min_max(args, kwds, Py_GT); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1078 | PyDoc_STRVAR(max_doc, |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1079 | "max(iterable[, key=func]) -> value\n\ |
| 1080 | max(a, b, c, ...[, key=func]) -> value\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1081 | \n\ |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1082 | With a single iterable argument, return its largest item.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1083 | With two or more arguments, return the largest argument."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1084 | |
| 1085 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1086 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1087 | builtin_oct(PyObject *self, PyObject *v) |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 1088 | { |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1089 | return PyNumber_ToBase(v, 8); |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1092 | PyDoc_STRVAR(oct_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1093 | "oct(number) -> string\n\ |
| 1094 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1095 | Return the octal representation of an integer or long integer."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1096 | |
| 1097 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1098 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1099 | builtin_ord(PyObject *self, PyObject* obj) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1100 | { |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 1101 | long ord; |
Martin v. Löwis | d96ee90 | 2006-02-16 14:37:16 +0000 | [diff] [blame] | 1102 | Py_ssize_t size; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1103 | |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1104 | if (PyString_Check(obj)) { |
| 1105 | size = PyString_GET_SIZE(obj); |
Andrew M. Kuchling | 34c20cf | 2000-12-20 14:36:56 +0000 | [diff] [blame] | 1106 | if (size == 1) { |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1107 | ord = (long)((unsigned char)*PyString_AS_STRING(obj)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1108 | return PyLong_FromLong(ord); |
Andrew M. Kuchling | 34c20cf | 2000-12-20 14:36:56 +0000 | [diff] [blame] | 1109 | } |
Guido van Rossum | 98f9746 | 2007-04-13 03:31:13 +0000 | [diff] [blame] | 1110 | } |
| 1111 | else if (PyUnicode_Check(obj)) { |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1112 | size = PyUnicode_GET_SIZE(obj); |
Andrew M. Kuchling | 34c20cf | 2000-12-20 14:36:56 +0000 | [diff] [blame] | 1113 | if (size == 1) { |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1114 | ord = (long)*PyUnicode_AS_UNICODE(obj); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1115 | return PyLong_FromLong(ord); |
Andrew M. Kuchling | 34c20cf | 2000-12-20 14:36:56 +0000 | [diff] [blame] | 1116 | } |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1117 | #ifndef Py_UNICODE_WIDE |
| 1118 | if (size == 2) { |
| 1119 | /* Decode a valid surrogate pair */ |
| 1120 | int c0 = PyUnicode_AS_UNICODE(obj)[0]; |
| 1121 | int c1 = PyUnicode_AS_UNICODE(obj)[1]; |
| 1122 | if (0xD800 <= c0 && c0 <= 0xDBFF && |
| 1123 | 0xDC00 <= c1 && c1 <= 0xDFFF) { |
| 1124 | ord = ((((c0 & 0x03FF) << 10) | (c1 & 0x03FF)) + |
| 1125 | 0x00010000); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1126 | return PyLong_FromLong(ord); |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1127 | } |
| 1128 | } |
| 1129 | #endif |
Guido van Rossum | 6f376c4 | 2007-05-24 14:31:33 +0000 | [diff] [blame] | 1130 | } |
Guido van Rossum | 98f9746 | 2007-04-13 03:31:13 +0000 | [diff] [blame] | 1131 | else if (PyBytes_Check(obj)) { |
| 1132 | /* XXX Hopefully this is temporary */ |
| 1133 | size = PyBytes_GET_SIZE(obj); |
| 1134 | if (size == 1) { |
Guido van Rossum | f9e91c9 | 2007-05-08 21:05:48 +0000 | [diff] [blame] | 1135 | ord = (long)((unsigned char)*PyBytes_AS_STRING(obj)); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1136 | return PyLong_FromLong(ord); |
Guido van Rossum | 98f9746 | 2007-04-13 03:31:13 +0000 | [diff] [blame] | 1137 | } |
| 1138 | } |
| 1139 | else { |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1140 | PyErr_Format(PyExc_TypeError, |
Andrew M. Kuchling | f07aad1 | 2000-12-23 14:11:28 +0000 | [diff] [blame] | 1141 | "ord() expected string of length 1, but " \ |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1142 | "%.200s found", obj->ob_type->tp_name); |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 1143 | return NULL; |
| 1144 | } |
| 1145 | |
Guido van Rossum | ad99177 | 2001-01-12 16:03:05 +0000 | [diff] [blame] | 1146 | PyErr_Format(PyExc_TypeError, |
Andrew M. Kuchling | f07aad1 | 2000-12-23 14:11:28 +0000 | [diff] [blame] | 1147 | "ord() expected a character, " |
Martin v. Löwis | d96ee90 | 2006-02-16 14:37:16 +0000 | [diff] [blame] | 1148 | "but string of length %zd found", |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1149 | size); |
| 1150 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
Guido van Rossum | 307fa8c | 2007-07-16 20:46:27 +0000 | [diff] [blame] | 1153 | PyDoc_VAR(ord_doc) = PyDoc_STR( |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1154 | "ord(c) -> integer\n\ |
| 1155 | \n\ |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1156 | Return the integer ordinal of a one-character string." |
Guido van Rossum | 307fa8c | 2007-07-16 20:46:27 +0000 | [diff] [blame] | 1157 | ) |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1158 | #ifndef Py_UNICODE_WIDE |
Guido van Rossum | 307fa8c | 2007-07-16 20:46:27 +0000 | [diff] [blame] | 1159 | PyDoc_STR( |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1160 | "\nA valid surrogate pair is also accepted." |
Guido van Rossum | 307fa8c | 2007-07-16 20:46:27 +0000 | [diff] [blame] | 1161 | ) |
Guido van Rossum | 8ac004e | 2007-07-15 13:00:05 +0000 | [diff] [blame] | 1162 | #endif |
Guido van Rossum | 307fa8c | 2007-07-16 20:46:27 +0000 | [diff] [blame] | 1163 | ; |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1164 | |
| 1165 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1166 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1167 | builtin_pow(PyObject *self, PyObject *args) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1168 | { |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 1169 | PyObject *v, *w, *z = Py_None; |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1170 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1171 | if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z)) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1172 | return NULL; |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 1173 | return PyNumber_Power(v, w, z); |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1176 | PyDoc_STRVAR(pow_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1177 | "pow(x, y[, z]) -> number\n\ |
| 1178 | \n\ |
| 1179 | 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] | 1180 | 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] | 1181 | |
| 1182 | |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1183 | |
Guido van Rossum | 3434351 | 2006-11-30 22:13:52 +0000 | [diff] [blame] | 1184 | static PyObject * |
| 1185 | builtin_print(PyObject *self, PyObject *args, PyObject *kwds) |
| 1186 | { |
| 1187 | static char *kwlist[] = {"sep", "end", "file", 0}; |
Georg Brandl | 257d3d9 | 2007-02-26 10:35:10 +0000 | [diff] [blame] | 1188 | static PyObject *dummy_args; |
Guido van Rossum | 3434351 | 2006-11-30 22:13:52 +0000 | [diff] [blame] | 1189 | PyObject *sep = NULL, *end = NULL, *file = NULL; |
| 1190 | int i, err; |
| 1191 | |
Georg Brandl | 257d3d9 | 2007-02-26 10:35:10 +0000 | [diff] [blame] | 1192 | if (dummy_args == NULL) { |
| 1193 | if (!(dummy_args = PyTuple_New(0))) |
| 1194 | return NULL; |
| 1195 | } |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 1196 | if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print", |
Guido van Rossum | 3434351 | 2006-11-30 22:13:52 +0000 | [diff] [blame] | 1197 | kwlist, &sep, &end, &file)) |
Christian Heimes | fef9bba | 2007-11-13 15:24:14 +0000 | [diff] [blame] | 1198 | return NULL; |
| 1199 | if (file == NULL || file == Py_None) { |
Guido van Rossum | 3434351 | 2006-11-30 22:13:52 +0000 | [diff] [blame] | 1200 | file = PySys_GetObject("stdout"); |
Christian Heimes | fef9bba | 2007-11-13 15:24:14 +0000 | [diff] [blame] | 1201 | /* sys.stdout may be None when FILE* stdout isn't connected */ |
| 1202 | if (file == Py_None) |
| 1203 | Py_RETURN_NONE; |
| 1204 | } |
Guido van Rossum | 3434351 | 2006-11-30 22:13:52 +0000 | [diff] [blame] | 1205 | |
Neal Norwitz | 6ea45d3 | 2007-08-26 04:19:43 +0000 | [diff] [blame] | 1206 | if (sep && sep != Py_None && !PyUnicode_Check(sep)) { |
Georg Brandl | 16f3e03 | 2006-11-30 22:46:03 +0000 | [diff] [blame] | 1207 | PyErr_Format(PyExc_TypeError, |
Neal Norwitz | 6ea45d3 | 2007-08-26 04:19:43 +0000 | [diff] [blame] | 1208 | "sep must be None or a string, not %.200s", |
Georg Brandl | 16f3e03 | 2006-11-30 22:46:03 +0000 | [diff] [blame] | 1209 | sep->ob_type->tp_name); |
| 1210 | return NULL; |
| 1211 | } |
Neal Norwitz | 6ea45d3 | 2007-08-26 04:19:43 +0000 | [diff] [blame] | 1212 | if (end && end != Py_None && !PyUnicode_Check(end)) { |
Georg Brandl | 16f3e03 | 2006-11-30 22:46:03 +0000 | [diff] [blame] | 1213 | PyErr_Format(PyExc_TypeError, |
Neal Norwitz | 6ea45d3 | 2007-08-26 04:19:43 +0000 | [diff] [blame] | 1214 | "end must be None or a string, not %.200s", |
Georg Brandl | 16f3e03 | 2006-11-30 22:46:03 +0000 | [diff] [blame] | 1215 | end->ob_type->tp_name); |
| 1216 | return NULL; |
| 1217 | } |
Guido van Rossum | 3434351 | 2006-11-30 22:13:52 +0000 | [diff] [blame] | 1218 | |
| 1219 | for (i = 0; i < PyTuple_Size(args); i++) { |
| 1220 | if (i > 0) { |
| 1221 | if (sep == NULL || sep == Py_None) |
| 1222 | err = PyFile_WriteString(" ", file); |
| 1223 | else |
| 1224 | err = PyFile_WriteObject(sep, file, |
| 1225 | Py_PRINT_RAW); |
| 1226 | if (err) |
| 1227 | return NULL; |
| 1228 | } |
| 1229 | err = PyFile_WriteObject(PyTuple_GetItem(args, i), file, |
| 1230 | Py_PRINT_RAW); |
| 1231 | if (err) |
| 1232 | return NULL; |
| 1233 | } |
| 1234 | |
| 1235 | if (end == NULL || end == Py_None) |
| 1236 | err = PyFile_WriteString("\n", file); |
| 1237 | else |
| 1238 | err = PyFile_WriteObject(end, file, Py_PRINT_RAW); |
| 1239 | if (err) |
| 1240 | return NULL; |
| 1241 | |
| 1242 | Py_RETURN_NONE; |
| 1243 | } |
| 1244 | |
| 1245 | PyDoc_STRVAR(print_doc, |
Guido van Rossum | 452bf51 | 2007-02-09 05:32:43 +0000 | [diff] [blame] | 1246 | "print(value, ..., file=None, sep=' ', end='\\n')\n\ |
Guido van Rossum | 3434351 | 2006-11-30 22:13:52 +0000 | [diff] [blame] | 1247 | \n\ |
| 1248 | Prints the values to a stream, or to sys.stdout by default.\n\ |
| 1249 | Optional keyword arguments:\n\ |
| 1250 | file: a file-like object (stream); defaults to the current sys.stdout.\n\ |
| 1251 | sep: string inserted between values, default a space.\n\ |
| 1252 | end: string appended after the last value, default a newline."); |
| 1253 | |
| 1254 | |
Guido van Rossum | a88a033 | 2007-02-26 16:59:55 +0000 | [diff] [blame] | 1255 | static PyObject * |
| 1256 | builtin_input(PyObject *self, PyObject *args) |
| 1257 | { |
Guido van Rossum | eba7696 | 2007-05-27 09:13:28 +0000 | [diff] [blame] | 1258 | PyObject *promptarg = NULL; |
Guido van Rossum | a88a033 | 2007-02-26 16:59:55 +0000 | [diff] [blame] | 1259 | PyObject *fin = PySys_GetObject("stdin"); |
| 1260 | PyObject *fout = PySys_GetObject("stdout"); |
Guido van Rossum | eba7696 | 2007-05-27 09:13:28 +0000 | [diff] [blame] | 1261 | PyObject *ferr = PySys_GetObject("stderr"); |
| 1262 | PyObject *tmp; |
| 1263 | long fd; |
| 1264 | int tty; |
Guido van Rossum | a88a033 | 2007-02-26 16:59:55 +0000 | [diff] [blame] | 1265 | |
Guido van Rossum | eba7696 | 2007-05-27 09:13:28 +0000 | [diff] [blame] | 1266 | /* Parse arguments */ |
| 1267 | if (!PyArg_UnpackTuple(args, "input", 0, 1, &promptarg)) |
Guido van Rossum | a88a033 | 2007-02-26 16:59:55 +0000 | [diff] [blame] | 1268 | return NULL; |
| 1269 | |
Guido van Rossum | eba7696 | 2007-05-27 09:13:28 +0000 | [diff] [blame] | 1270 | /* Check that stdin/out/err are intact */ |
Christian Heimes | 2be0373 | 2007-11-15 02:26:46 +0000 | [diff] [blame] | 1271 | if (fin == NULL || fin == Py_None) { |
Guido van Rossum | eba7696 | 2007-05-27 09:13:28 +0000 | [diff] [blame] | 1272 | PyErr_SetString(PyExc_RuntimeError, |
| 1273 | "input(): lost sys.stdin"); |
Guido van Rossum | a88a033 | 2007-02-26 16:59:55 +0000 | [diff] [blame] | 1274 | return NULL; |
| 1275 | } |
Christian Heimes | 2be0373 | 2007-11-15 02:26:46 +0000 | [diff] [blame] | 1276 | if (fout == NULL || fout == Py_None) { |
Guido van Rossum | eba7696 | 2007-05-27 09:13:28 +0000 | [diff] [blame] | 1277 | PyErr_SetString(PyExc_RuntimeError, |
| 1278 | "input(): lost sys.stdout"); |
Guido van Rossum | a88a033 | 2007-02-26 16:59:55 +0000 | [diff] [blame] | 1279 | return NULL; |
| 1280 | } |
Christian Heimes | 2be0373 | 2007-11-15 02:26:46 +0000 | [diff] [blame] | 1281 | if (ferr == NULL || ferr == Py_None) { |
Guido van Rossum | eba7696 | 2007-05-27 09:13:28 +0000 | [diff] [blame] | 1282 | PyErr_SetString(PyExc_RuntimeError, |
| 1283 | "input(): lost sys.stderr"); |
| 1284 | return NULL; |
| 1285 | } |
| 1286 | |
| 1287 | /* First of all, flush stderr */ |
| 1288 | tmp = PyObject_CallMethod(ferr, "flush", ""); |
| 1289 | if (tmp == NULL) |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 1290 | PyErr_Clear(); |
| 1291 | else |
| 1292 | Py_DECREF(tmp); |
Guido van Rossum | eba7696 | 2007-05-27 09:13:28 +0000 | [diff] [blame] | 1293 | |
| 1294 | /* We should only use (GNU) readline if Python's sys.stdin and |
| 1295 | sys.stdout are the same as C's stdin and stdout, because we |
| 1296 | need to pass it those. */ |
| 1297 | tmp = PyObject_CallMethod(fin, "fileno", ""); |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 1298 | if (tmp == NULL) { |
| 1299 | PyErr_Clear(); |
| 1300 | tty = 0; |
| 1301 | } |
| 1302 | else { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1303 | fd = PyLong_AsLong(tmp); |
Guido van Rossum | eba7696 | 2007-05-27 09:13:28 +0000 | [diff] [blame] | 1304 | Py_DECREF(tmp); |
| 1305 | if (fd < 0 && PyErr_Occurred()) |
| 1306 | return NULL; |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 1307 | tty = fd == fileno(stdin) && isatty(fd); |
| 1308 | } |
| 1309 | if (tty) { |
| 1310 | tmp = PyObject_CallMethod(fout, "fileno", ""); |
| 1311 | if (tmp == NULL) |
| 1312 | PyErr_Clear(); |
| 1313 | else { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1314 | fd = PyLong_AsLong(tmp); |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 1315 | Py_DECREF(tmp); |
| 1316 | if (fd < 0 && PyErr_Occurred()) |
| 1317 | return NULL; |
| 1318 | tty = fd == fileno(stdout) && isatty(fd); |
| 1319 | } |
Guido van Rossum | eba7696 | 2007-05-27 09:13:28 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | /* If we're interactive, use (GNU) readline */ |
| 1323 | if (tty) { |
Guido van Rossum | a88a033 | 2007-02-26 16:59:55 +0000 | [diff] [blame] | 1324 | PyObject *po; |
| 1325 | char *prompt; |
| 1326 | char *s; |
Martin v. Löwis | 4a7b5d5 | 2007-09-04 05:24:49 +0000 | [diff] [blame] | 1327 | PyObject *stdin_encoding; |
Guido van Rossum | a88a033 | 2007-02-26 16:59:55 +0000 | [diff] [blame] | 1328 | PyObject *result; |
Martin v. Löwis | 4a7b5d5 | 2007-09-04 05:24:49 +0000 | [diff] [blame] | 1329 | |
| 1330 | stdin_encoding = PyObject_GetAttrString(fin, "encoding"); |
| 1331 | if (!stdin_encoding) |
| 1332 | /* stdin is a text stream, so it must have an |
| 1333 | encoding. */ |
| 1334 | return NULL; |
Guido van Rossum | eba7696 | 2007-05-27 09:13:28 +0000 | [diff] [blame] | 1335 | tmp = PyObject_CallMethod(fout, "flush", ""); |
| 1336 | if (tmp == NULL) |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 1337 | PyErr_Clear(); |
| 1338 | else |
| 1339 | Py_DECREF(tmp); |
Guido van Rossum | eba7696 | 2007-05-27 09:13:28 +0000 | [diff] [blame] | 1340 | if (promptarg != NULL) { |
| 1341 | po = PyObject_Str(promptarg); |
Martin v. Löwis | 4a7b5d5 | 2007-09-04 05:24:49 +0000 | [diff] [blame] | 1342 | if (po == NULL) { |
| 1343 | Py_DECREF(stdin_encoding); |
Guido van Rossum | a88a033 | 2007-02-26 16:59:55 +0000 | [diff] [blame] | 1344 | return NULL; |
Martin v. Löwis | 4a7b5d5 | 2007-09-04 05:24:49 +0000 | [diff] [blame] | 1345 | } |
Christian Heimes | 91c7730 | 2007-11-25 09:18:01 +0000 | [diff] [blame] | 1346 | prompt = PyUnicode_AsString(po); |
Guido van Rossum | eba7696 | 2007-05-27 09:13:28 +0000 | [diff] [blame] | 1347 | if (prompt == NULL) { |
Martin v. Löwis | 4a7b5d5 | 2007-09-04 05:24:49 +0000 | [diff] [blame] | 1348 | Py_DECREF(stdin_encoding); |
Guido van Rossum | eba7696 | 2007-05-27 09:13:28 +0000 | [diff] [blame] | 1349 | Py_DECREF(po); |
Guido van Rossum | a88a033 | 2007-02-26 16:59:55 +0000 | [diff] [blame] | 1350 | return NULL; |
Guido van Rossum | eba7696 | 2007-05-27 09:13:28 +0000 | [diff] [blame] | 1351 | } |
Guido van Rossum | a88a033 | 2007-02-26 16:59:55 +0000 | [diff] [blame] | 1352 | } |
| 1353 | else { |
| 1354 | po = NULL; |
| 1355 | prompt = ""; |
| 1356 | } |
Guido van Rossum | eba7696 | 2007-05-27 09:13:28 +0000 | [diff] [blame] | 1357 | s = PyOS_Readline(stdin, stdout, prompt); |
Guido van Rossum | a88a033 | 2007-02-26 16:59:55 +0000 | [diff] [blame] | 1358 | Py_XDECREF(po); |
| 1359 | if (s == NULL) { |
| 1360 | if (!PyErr_Occurred()) |
| 1361 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
Martin v. Löwis | 4a7b5d5 | 2007-09-04 05:24:49 +0000 | [diff] [blame] | 1362 | Py_DECREF(stdin_encoding); |
Guido van Rossum | a88a033 | 2007-02-26 16:59:55 +0000 | [diff] [blame] | 1363 | return NULL; |
| 1364 | } |
| 1365 | if (*s == '\0') { |
| 1366 | PyErr_SetNone(PyExc_EOFError); |
| 1367 | result = NULL; |
| 1368 | } |
| 1369 | else { /* strip trailing '\n' */ |
| 1370 | size_t len = strlen(s); |
| 1371 | if (len > PY_SSIZE_T_MAX) { |
| 1372 | PyErr_SetString(PyExc_OverflowError, |
| 1373 | "input: input too long"); |
| 1374 | result = NULL; |
| 1375 | } |
| 1376 | else { |
Martin v. Löwis | 4a7b5d5 | 2007-09-04 05:24:49 +0000 | [diff] [blame] | 1377 | result = PyUnicode_Decode |
| 1378 | (s, len-1, |
| 1379 | PyUnicode_AsString(stdin_encoding), |
| 1380 | NULL); |
Guido van Rossum | a88a033 | 2007-02-26 16:59:55 +0000 | [diff] [blame] | 1381 | } |
| 1382 | } |
Martin v. Löwis | 4a7b5d5 | 2007-09-04 05:24:49 +0000 | [diff] [blame] | 1383 | Py_DECREF(stdin_encoding); |
Guido van Rossum | a88a033 | 2007-02-26 16:59:55 +0000 | [diff] [blame] | 1384 | PyMem_FREE(s); |
| 1385 | return result; |
| 1386 | } |
Guido van Rossum | eba7696 | 2007-05-27 09:13:28 +0000 | [diff] [blame] | 1387 | |
| 1388 | /* Fallback if we're not interactive */ |
| 1389 | if (promptarg != NULL) { |
| 1390 | if (PyFile_WriteObject(promptarg, fout, Py_PRINT_RAW) != 0) |
Guido van Rossum | a88a033 | 2007-02-26 16:59:55 +0000 | [diff] [blame] | 1391 | return NULL; |
| 1392 | } |
Guido van Rossum | eba7696 | 2007-05-27 09:13:28 +0000 | [diff] [blame] | 1393 | tmp = PyObject_CallMethod(fout, "flush", ""); |
| 1394 | if (tmp == NULL) |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 1395 | PyErr_Clear(); |
| 1396 | else |
| 1397 | Py_DECREF(tmp); |
Guido van Rossum | a88a033 | 2007-02-26 16:59:55 +0000 | [diff] [blame] | 1398 | return PyFile_GetLine(fin, -1); |
| 1399 | } |
| 1400 | |
| 1401 | PyDoc_STRVAR(input_doc, |
| 1402 | "input([prompt]) -> string\n\ |
| 1403 | \n\ |
| 1404 | Read a string from standard input. The trailing newline is stripped.\n\ |
| 1405 | If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\ |
| 1406 | On Unix, GNU readline is used if enabled. The prompt string, if given,\n\ |
| 1407 | is printed without a trailing newline before reading."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1408 | |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1409 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1410 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1411 | builtin_repr(PyObject *self, PyObject *v) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1412 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1413 | return PyObject_Repr(v); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1416 | PyDoc_STRVAR(repr_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1417 | "repr(object) -> string\n\ |
| 1418 | \n\ |
| 1419 | Return the canonical string representation of the object.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1420 | For most object types, eval(repr(object)) == object."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1421 | |
| 1422 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1423 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1424 | builtin_round(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1425 | { |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 1426 | #define UNDEF_NDIGITS (-0x7fffffff) /* Unlikely ndigits value */ |
| 1427 | static PyObject *round_str = NULL; |
| 1428 | int ndigits = UNDEF_NDIGITS; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1429 | static char *kwlist[] = {"number", "ndigits", 0}; |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 1430 | PyObject *number, *round; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1431 | |
Alex Martelli | ae211f9 | 2007-08-22 23:21:33 +0000 | [diff] [blame] | 1432 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:round", |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 1433 | kwlist, &number, &ndigits)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1434 | return NULL; |
Alex Martelli | ae211f9 | 2007-08-22 23:21:33 +0000 | [diff] [blame] | 1435 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame^] | 1436 | if (Py_TYPE(number)->tp_dict == NULL) { |
| 1437 | if (PyType_Ready(Py_TYPE(number)) < 0) |
Guido van Rossum | 15d3d04 | 2007-08-24 02:02:45 +0000 | [diff] [blame] | 1438 | return NULL; |
| 1439 | } |
| 1440 | |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 1441 | if (round_str == NULL) { |
| 1442 | round_str = PyUnicode_FromString("__round__"); |
| 1443 | if (round_str == NULL) |
Alex Martelli | ae211f9 | 2007-08-22 23:21:33 +0000 | [diff] [blame] | 1444 | return NULL; |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame^] | 1447 | round = _PyType_Lookup(Py_TYPE(number), round_str); |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 1448 | if (round == NULL) { |
| 1449 | PyErr_Format(PyExc_TypeError, |
| 1450 | "type %.100s doesn't define __round__ method", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame^] | 1451 | Py_TYPE(number)->tp_name); |
Alex Martelli | ae211f9 | 2007-08-22 23:21:33 +0000 | [diff] [blame] | 1452 | return NULL; |
| 1453 | } |
| 1454 | |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 1455 | if (ndigits == UNDEF_NDIGITS) |
| 1456 | return PyObject_CallFunction(round, "O", number); |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1457 | else |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 1458 | return PyObject_CallFunction(round, "Oi", number, ndigits); |
| 1459 | #undef UNDEF_NDIGITS |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1462 | PyDoc_STRVAR(round_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1463 | "round(number[, ndigits]) -> floating point number\n\ |
| 1464 | \n\ |
| 1465 | Round a number to a given precision in decimal digits (default 0 digits).\n\ |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 1466 | This returns an int when called with one argument, otherwise a float.\n\ |
| 1467 | Precision may be negative."); |
| 1468 | |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1469 | |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 1470 | static PyObject * |
| 1471 | builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds) |
| 1472 | { |
| 1473 | PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs; |
| 1474 | PyObject *callable; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 1475 | static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0}; |
Neal Norwitz | 6f0d479 | 2005-12-15 06:40:36 +0000 | [diff] [blame] | 1476 | int reverse; |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 1477 | |
Neal Norwitz | 6f0d479 | 2005-12-15 06:40:36 +0000 | [diff] [blame] | 1478 | /* args 1-4 should match listsort in Objects/listobject.c */ |
Armin Rigo | 71d7e70 | 2005-09-20 18:13:03 +0000 | [diff] [blame] | 1479 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted", |
| 1480 | kwlist, &seq, &compare, &keyfunc, &reverse)) |
| 1481 | return NULL; |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 1482 | |
| 1483 | newlist = PySequence_List(seq); |
| 1484 | if (newlist == NULL) |
| 1485 | return NULL; |
| 1486 | |
| 1487 | callable = PyObject_GetAttrString(newlist, "sort"); |
| 1488 | if (callable == NULL) { |
| 1489 | Py_DECREF(newlist); |
| 1490 | return NULL; |
| 1491 | } |
Georg Brandl | 99d7e4e | 2005-08-31 22:21:15 +0000 | [diff] [blame] | 1492 | |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 1493 | newargs = PyTuple_GetSlice(args, 1, 4); |
| 1494 | if (newargs == NULL) { |
| 1495 | Py_DECREF(newlist); |
| 1496 | Py_DECREF(callable); |
| 1497 | return NULL; |
| 1498 | } |
| 1499 | |
| 1500 | v = PyObject_Call(callable, newargs, kwds); |
| 1501 | Py_DECREF(newargs); |
| 1502 | Py_DECREF(callable); |
| 1503 | if (v == NULL) { |
| 1504 | Py_DECREF(newlist); |
| 1505 | return NULL; |
| 1506 | } |
| 1507 | Py_DECREF(v); |
| 1508 | return newlist; |
| 1509 | } |
| 1510 | |
| 1511 | PyDoc_STRVAR(sorted_doc, |
| 1512 | "sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list"); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1513 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1514 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1515 | builtin_vars(PyObject *self, PyObject *args) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1516 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1517 | PyObject *v = NULL; |
| 1518 | PyObject *d; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1519 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1520 | if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1521 | return NULL; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1522 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1523 | d = PyEval_GetLocals(); |
Guido van Rossum | 53bb7ff | 1995-07-26 16:26:31 +0000 | [diff] [blame] | 1524 | if (d == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1525 | if (!PyErr_Occurred()) |
| 1526 | PyErr_SetString(PyExc_SystemError, |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 1527 | "vars(): no locals!?"); |
Guido van Rossum | 53bb7ff | 1995-07-26 16:26:31 +0000 | [diff] [blame] | 1528 | } |
| 1529 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1530 | Py_INCREF(d); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1531 | } |
| 1532 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1533 | d = PyObject_GetAttrString(v, "__dict__"); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1534 | if (d == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1535 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1536 | "vars() argument must have __dict__ attribute"); |
| 1537 | return NULL; |
| 1538 | } |
| 1539 | } |
| 1540 | return d; |
| 1541 | } |
| 1542 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1543 | PyDoc_STRVAR(vars_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1544 | "vars([object]) -> dictionary\n\ |
| 1545 | \n\ |
| 1546 | Without arguments, equivalent to locals().\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1547 | With an argument, equivalent to object.__dict__."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1548 | |
Alex Martelli | 86d8b34 | 2007-08-22 22:39:42 +0000 | [diff] [blame] | 1549 | static PyObject * |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 1550 | builtin_trunc(PyObject *self, PyObject *number) |
Alex Martelli | 86d8b34 | 2007-08-22 22:39:42 +0000 | [diff] [blame] | 1551 | { |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 1552 | static PyObject *trunc_str = NULL; |
| 1553 | PyObject *trunc; |
| 1554 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame^] | 1555 | if (Py_TYPE(number)->tp_dict == NULL) { |
| 1556 | if (PyType_Ready(Py_TYPE(number)) < 0) |
Guido van Rossum | 15d3d04 | 2007-08-24 02:02:45 +0000 | [diff] [blame] | 1557 | return NULL; |
| 1558 | } |
| 1559 | |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 1560 | if (trunc_str == NULL) { |
| 1561 | trunc_str = PyUnicode_FromString("__trunc__"); |
| 1562 | if (trunc_str == NULL) |
| 1563 | return NULL; |
| 1564 | } |
| 1565 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame^] | 1566 | trunc = _PyType_Lookup(Py_TYPE(number), trunc_str); |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 1567 | if (trunc == NULL) { |
| 1568 | PyErr_Format(PyExc_TypeError, |
| 1569 | "type %.100s doesn't define __trunc__ method", |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame^] | 1570 | Py_TYPE(number)->tp_name); |
Alex Martelli | 86d8b34 | 2007-08-22 22:39:42 +0000 | [diff] [blame] | 1571 | return NULL; |
| 1572 | } |
Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 1573 | return PyObject_CallFunction(trunc, "O", number); |
Alex Martelli | 86d8b34 | 2007-08-22 22:39:42 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | PyDoc_STRVAR(trunc_doc, |
| 1577 | "trunc(Real) -> Integral\n\ |
| 1578 | \n\ |
| 1579 | returns the integral closest to x between 0 and x."); |
| 1580 | |
| 1581 | |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 1582 | |
| 1583 | static PyObject* |
| 1584 | builtin_sum(PyObject *self, PyObject *args) |
| 1585 | { |
| 1586 | PyObject *seq; |
| 1587 | PyObject *result = NULL; |
| 1588 | PyObject *temp, *item, *iter; |
| 1589 | |
Raymond Hettinger | 5cf6394 | 2003-10-25 06:41:37 +0000 | [diff] [blame] | 1590 | if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result)) |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 1591 | return NULL; |
| 1592 | |
| 1593 | iter = PyObject_GetIter(seq); |
| 1594 | if (iter == NULL) |
| 1595 | return NULL; |
| 1596 | |
| 1597 | if (result == NULL) { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1598 | result = PyLong_FromLong(0); |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 1599 | if (result == NULL) { |
| 1600 | Py_DECREF(iter); |
| 1601 | return NULL; |
| 1602 | } |
| 1603 | } else { |
| 1604 | /* reject string values for 'start' parameter */ |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 1605 | if (PyUnicode_Check(result)) { |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 1606 | PyErr_SetString(PyExc_TypeError, |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 1607 | "sum() can't sum strings [use ''.join(seq) instead]"); |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 1608 | Py_DECREF(iter); |
| 1609 | return NULL; |
| 1610 | } |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 1611 | if (PyBytes_Check(result)) { |
| 1612 | PyErr_SetString(PyExc_TypeError, |
| 1613 | "sum() can't sum bytes [use b''.join(seq) instead]"); |
| 1614 | Py_DECREF(iter); |
| 1615 | return NULL; |
| 1616 | } |
| 1617 | |
Alex Martelli | 41c9f88 | 2003-04-22 09:24:48 +0000 | [diff] [blame] | 1618 | Py_INCREF(result); |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1621 | #ifndef SLOW_SUM |
| 1622 | /* Fast addition by keeping temporary sums in C instead of new Python objects. |
| 1623 | Assumes all inputs are the same type. If the assumption fails, default |
| 1624 | to the more general routine. |
| 1625 | */ |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 1626 | if (PyLong_CheckExact(result)) { |
| 1627 | int overflow; |
| 1628 | long i_result = PyLong_AsLongAndOverflow(result, &overflow); |
| 1629 | /* If this already overflowed, don't even enter the loop. */ |
| 1630 | if (overflow == 0) { |
| 1631 | Py_DECREF(result); |
| 1632 | result = NULL; |
| 1633 | } |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1634 | while(result == NULL) { |
| 1635 | item = PyIter_Next(iter); |
| 1636 | if (item == NULL) { |
| 1637 | Py_DECREF(iter); |
| 1638 | if (PyErr_Occurred()) |
| 1639 | return NULL; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1640 | return PyLong_FromLong(i_result); |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1641 | } |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 1642 | if (PyLong_CheckExact(item)) { |
| 1643 | long b = PyLong_AsLongAndOverflow(item, &overflow); |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1644 | long x = i_result + b; |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 1645 | if (overflow == 0 && ((x^i_result) >= 0 || (x^b) >= 0)) { |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1646 | i_result = x; |
| 1647 | Py_DECREF(item); |
| 1648 | continue; |
| 1649 | } |
| 1650 | } |
| 1651 | /* Either overflowed or is not an int. Restore real objects and process normally */ |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1652 | result = PyLong_FromLong(i_result); |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1653 | temp = PyNumber_Add(result, item); |
| 1654 | Py_DECREF(result); |
| 1655 | Py_DECREF(item); |
| 1656 | result = temp; |
| 1657 | if (result == NULL) { |
| 1658 | Py_DECREF(iter); |
| 1659 | return NULL; |
| 1660 | } |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | if (PyFloat_CheckExact(result)) { |
| 1665 | double f_result = PyFloat_AS_DOUBLE(result); |
| 1666 | Py_DECREF(result); |
| 1667 | result = NULL; |
| 1668 | while(result == NULL) { |
| 1669 | item = PyIter_Next(iter); |
| 1670 | if (item == NULL) { |
| 1671 | Py_DECREF(iter); |
| 1672 | if (PyErr_Occurred()) |
| 1673 | return NULL; |
| 1674 | return PyFloat_FromDouble(f_result); |
| 1675 | } |
| 1676 | if (PyFloat_CheckExact(item)) { |
| 1677 | PyFPE_START_PROTECT("add", return 0) |
| 1678 | f_result += PyFloat_AS_DOUBLE(item); |
| 1679 | PyFPE_END_PROTECT(f_result) |
| 1680 | Py_DECREF(item); |
| 1681 | continue; |
| 1682 | } |
Martin v. Löwis | d1a1d1e | 2007-12-04 22:10:37 +0000 | [diff] [blame] | 1683 | if (PyLong_CheckExact(item)) { |
| 1684 | long value; |
| 1685 | int overflow; |
| 1686 | value = PyLong_AsLongAndOverflow(item, &overflow); |
| 1687 | if (!overflow) { |
| 1688 | PyFPE_START_PROTECT("add", return 0) |
| 1689 | f_result += (double)value; |
| 1690 | PyFPE_END_PROTECT(f_result) |
| 1691 | Py_DECREF(item); |
| 1692 | continue; |
| 1693 | } |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1694 | } |
| 1695 | result = PyFloat_FromDouble(f_result); |
| 1696 | temp = PyNumber_Add(result, item); |
| 1697 | Py_DECREF(result); |
| 1698 | Py_DECREF(item); |
| 1699 | result = temp; |
| 1700 | if (result == NULL) { |
| 1701 | Py_DECREF(iter); |
| 1702 | return NULL; |
| 1703 | } |
| 1704 | } |
| 1705 | } |
| 1706 | #endif |
| 1707 | |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 1708 | for(;;) { |
| 1709 | item = PyIter_Next(iter); |
| 1710 | if (item == NULL) { |
| 1711 | /* error, or end-of-sequence */ |
| 1712 | if (PyErr_Occurred()) { |
| 1713 | Py_DECREF(result); |
| 1714 | result = NULL; |
| 1715 | } |
| 1716 | break; |
| 1717 | } |
Alex Martelli | a253e18 | 2003-10-25 23:24:14 +0000 | [diff] [blame] | 1718 | temp = PyNumber_Add(result, item); |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 1719 | Py_DECREF(result); |
| 1720 | Py_DECREF(item); |
| 1721 | result = temp; |
| 1722 | if (result == NULL) |
| 1723 | break; |
| 1724 | } |
| 1725 | Py_DECREF(iter); |
| 1726 | return result; |
| 1727 | } |
| 1728 | |
| 1729 | PyDoc_STRVAR(sum_doc, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1730 | "sum(sequence[, start]) -> value\n\ |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 1731 | \n\ |
| 1732 | Returns the sum of a sequence of numbers (NOT strings) plus the value\n\ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1733 | of parameter 'start' (which defaults to 0). When the sequence is\n\ |
| 1734 | empty, returns start."); |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 1735 | |
| 1736 | |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1737 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1738 | builtin_isinstance(PyObject *self, PyObject *args) |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1739 | { |
| 1740 | PyObject *inst; |
| 1741 | PyObject *cls; |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 1742 | int retval; |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1743 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1744 | if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls)) |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1745 | return NULL; |
Guido van Rossum | f5dd914 | 1997-12-02 19:11:45 +0000 | [diff] [blame] | 1746 | |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 1747 | retval = PyObject_IsInstance(inst, cls); |
| 1748 | if (retval < 0) |
Guido van Rossum | ad99177 | 2001-01-12 16:03:05 +0000 | [diff] [blame] | 1749 | return NULL; |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1750 | return PyBool_FromLong(retval); |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1753 | PyDoc_STRVAR(isinstance_doc, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1754 | "isinstance(object, class-or-type-or-tuple) -> bool\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1755 | \n\ |
| 1756 | 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] | 1757 | With a type as second argument, return whether that is the object's type.\n\ |
| 1758 | 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] | 1759 | isinstance(x, A) or isinstance(x, B) or ... (etc.)."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1760 | |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1761 | |
| 1762 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1763 | builtin_issubclass(PyObject *self, PyObject *args) |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1764 | { |
| 1765 | PyObject *derived; |
| 1766 | PyObject *cls; |
| 1767 | int retval; |
| 1768 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1769 | if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls)) |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1770 | return NULL; |
Guido van Rossum | 668213d | 1999-06-16 17:28:37 +0000 | [diff] [blame] | 1771 | |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 1772 | retval = PyObject_IsSubclass(derived, cls); |
| 1773 | if (retval < 0) |
| 1774 | return NULL; |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1775 | return PyBool_FromLong(retval); |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1776 | } |
| 1777 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1778 | PyDoc_STRVAR(issubclass_doc, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1779 | "issubclass(C, B) -> bool\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1780 | \n\ |
Walter Dörwald | d9a6ad3 | 2002-12-12 16:41:44 +0000 | [diff] [blame] | 1781 | Return whether class C is a subclass (i.e., a derived class) of class B.\n\ |
| 1782 | When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\ |
| 1783 | 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] | 1784 | |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1785 | |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1786 | static PyObject* |
| 1787 | builtin_zip(PyObject *self, PyObject *args) |
| 1788 | { |
Guido van Rossum | b65fb33 | 2006-08-25 23:26:40 +0000 | [diff] [blame] | 1789 | /* args must be a tuple */ |
| 1790 | assert(PyTuple_Check(args)); |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1791 | |
Guido van Rossum | b65fb33 | 2006-08-25 23:26:40 +0000 | [diff] [blame] | 1792 | return _PyZip_CreateIter(args); |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1796 | PyDoc_STRVAR(zip_doc, |
Guido van Rossum | 801f0d7 | 2006-08-24 19:48:10 +0000 | [diff] [blame] | 1797 | "zip(it1 [, it2 [...]]) -> iter([(it1[0], it2[0] ...), ...])\n\ |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1798 | \n\ |
Guido van Rossum | 801f0d7 | 2006-08-24 19:48:10 +0000 | [diff] [blame] | 1799 | Return an iterator yielding tuples, where each tuple contains the\n\ |
| 1800 | corresponding element from each of the argument iterables.\n\ |
| 1801 | The returned iterator ends when the shortest argument iterable is exhausted.\n\ |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 1802 | (This is identical to itertools.izip().)"); |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1803 | |
| 1804 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1805 | static PyMethodDef builtin_methods[] = { |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 1806 | {"__build_class__", (PyCFunction)builtin___build_class__, |
| 1807 | METH_VARARGS | METH_KEYWORDS, build_class_doc}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1808 | {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1809 | {"abs", builtin_abs, METH_O, abs_doc}, |
Raymond Hettinger | 96229b1 | 2005-03-11 06:49:40 +0000 | [diff] [blame] | 1810 | {"all", builtin_all, METH_O, all_doc}, |
| 1811 | {"any", builtin_any, METH_O, any_doc}, |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1812 | {"bin", builtin_bin, METH_O, bin_doc}, |
Neal Norwitz | c2550c7 | 2007-08-31 04:17:51 +0000 | [diff] [blame] | 1813 | {"chr", builtin_chr, METH_VARARGS, chr_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1814 | {"cmp", builtin_cmp, METH_VARARGS, cmp_doc}, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1815 | {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1816 | {"delattr", builtin_delattr, METH_VARARGS, delattr_doc}, |
| 1817 | {"dir", builtin_dir, METH_VARARGS, dir_doc}, |
| 1818 | {"divmod", builtin_divmod, METH_VARARGS, divmod_doc}, |
| 1819 | {"eval", builtin_eval, METH_VARARGS, eval_doc}, |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 1820 | {"exec", builtin_exec, METH_VARARGS, exec_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1821 | {"filter", builtin_filter, METH_VARARGS, filter_doc}, |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 1822 | {"format", builtin_format, METH_VARARGS, format_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1823 | {"getattr", builtin_getattr, METH_VARARGS, getattr_doc}, |
| 1824 | {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc}, |
| 1825 | {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc}, |
| 1826 | {"hash", builtin_hash, METH_O, hash_doc}, |
| 1827 | {"hex", builtin_hex, METH_O, hex_doc}, |
| 1828 | {"id", builtin_id, METH_O, id_doc}, |
Guido van Rossum | a88a033 | 2007-02-26 16:59:55 +0000 | [diff] [blame] | 1829 | {"input", builtin_input, METH_VARARGS, input_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1830 | {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc}, |
| 1831 | {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc}, |
| 1832 | {"iter", builtin_iter, METH_VARARGS, iter_doc}, |
| 1833 | {"len", builtin_len, METH_O, len_doc}, |
| 1834 | {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc}, |
| 1835 | {"map", builtin_map, METH_VARARGS, map_doc}, |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1836 | {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc}, |
| 1837 | {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc}, |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 1838 | {"next", (PyCFunction)builtin_next, METH_VARARGS, next_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1839 | {"oct", builtin_oct, METH_O, oct_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1840 | {"ord", builtin_ord, METH_O, ord_doc}, |
| 1841 | {"pow", builtin_pow, METH_VARARGS, pow_doc}, |
Guido van Rossum | 452bf51 | 2007-02-09 05:32:43 +0000 | [diff] [blame] | 1842 | {"print", (PyCFunction)builtin_print, METH_VARARGS | METH_KEYWORDS, print_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1843 | {"repr", builtin_repr, METH_O, repr_doc}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1844 | {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1845 | {"setattr", builtin_setattr, METH_VARARGS, setattr_doc}, |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 1846 | {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc}, |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 1847 | {"sum", builtin_sum, METH_VARARGS, sum_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1848 | {"vars", builtin_vars, METH_VARARGS, vars_doc}, |
Alex Martelli | 86d8b34 | 2007-08-22 22:39:42 +0000 | [diff] [blame] | 1849 | {"trunc", builtin_trunc, METH_O, trunc_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1850 | {"zip", builtin_zip, METH_VARARGS, zip_doc}, |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 1851 | {NULL, NULL}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1852 | }; |
| 1853 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1854 | PyDoc_STRVAR(builtin_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1855 | "Built-in functions, exceptions, and other objects.\n\ |
| 1856 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1857 | Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1858 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1859 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1860 | _PyBuiltin_Init(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1861 | { |
Fred Drake | 5550de3 | 2000-06-20 04:54:19 +0000 | [diff] [blame] | 1862 | PyObject *mod, *dict, *debug; |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 1863 | mod = Py_InitModule4("builtins", builtin_methods, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1864 | builtin_doc, (PyObject *)NULL, |
| 1865 | PYTHON_API_VERSION); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1866 | if (mod == NULL) |
| 1867 | return NULL; |
| 1868 | dict = PyModule_GetDict(mod); |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 1869 | |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 1870 | #ifdef Py_TRACE_REFS |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 1871 | /* "builtins" exposes a number of statically allocated objects |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 1872 | * that, before this code was added in 2.3, never showed up in |
| 1873 | * the list of "all objects" maintained by Py_TRACE_REFS. As a |
| 1874 | * result, programs leaking references to None and False (etc) |
| 1875 | * couldn't be diagnosed by examining sys.getobjects(0). |
| 1876 | */ |
| 1877 | #define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0) |
| 1878 | #else |
| 1879 | #define ADD_TO_ALL(OBJECT) (void)0 |
| 1880 | #endif |
| 1881 | |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 1882 | #define SETBUILTIN(NAME, OBJECT) \ |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 1883 | if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \ |
| 1884 | return NULL; \ |
| 1885 | ADD_TO_ALL(OBJECT) |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 1886 | |
| 1887 | SETBUILTIN("None", Py_None); |
| 1888 | SETBUILTIN("Ellipsis", Py_Ellipsis); |
| 1889 | SETBUILTIN("NotImplemented", Py_NotImplemented); |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1890 | SETBUILTIN("False", Py_False); |
| 1891 | SETBUILTIN("True", Py_True); |
| 1892 | SETBUILTIN("bool", &PyBool_Type); |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 1893 | SETBUILTIN("memoryview", &PyMemoryView_Type); |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 1894 | SETBUILTIN("bytearray", &PyBytes_Type); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1895 | SETBUILTIN("bytes", &PyString_Type); |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 1896 | SETBUILTIN("classmethod", &PyClassMethod_Type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1897 | #ifndef WITHOUT_COMPLEX |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 1898 | SETBUILTIN("complex", &PyComplex_Type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1899 | #endif |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 1900 | SETBUILTIN("dict", &PyDict_Type); |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1901 | SETBUILTIN("enumerate", &PyEnum_Type); |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 1902 | SETBUILTIN("float", &PyFloat_Type); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1903 | SETBUILTIN("frozenset", &PyFrozenSet_Type); |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 1904 | SETBUILTIN("property", &PyProperty_Type); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1905 | SETBUILTIN("int", &PyLong_Type); |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 1906 | SETBUILTIN("list", &PyList_Type); |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 1907 | SETBUILTIN("object", &PyBaseObject_Type); |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1908 | SETBUILTIN("range", &PyRange_Type); |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 1909 | SETBUILTIN("reversed", &PyReversed_Type); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 1910 | SETBUILTIN("set", &PySet_Type); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 1911 | SETBUILTIN("slice", &PySlice_Type); |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 1912 | SETBUILTIN("staticmethod", &PyStaticMethod_Type); |
Guido van Rossum | 572dbf8 | 2007-04-27 23:53:51 +0000 | [diff] [blame] | 1913 | SETBUILTIN("str", &PyUnicode_Type); |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 1914 | SETBUILTIN("super", &PySuper_Type); |
| 1915 | SETBUILTIN("tuple", &PyTuple_Type); |
| 1916 | SETBUILTIN("type", &PyType_Type); |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1917 | debug = PyBool_FromLong(Py_OptimizeFlag == 0); |
Fred Drake | 5550de3 | 2000-06-20 04:54:19 +0000 | [diff] [blame] | 1918 | if (PyDict_SetItemString(dict, "__debug__", debug) < 0) { |
| 1919 | Py_XDECREF(debug); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1920 | return NULL; |
Fred Drake | 5550de3 | 2000-06-20 04:54:19 +0000 | [diff] [blame] | 1921 | } |
| 1922 | Py_XDECREF(debug); |
Barry Warsaw | 757af0e | 1997-08-29 22:13:51 +0000 | [diff] [blame] | 1923 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1924 | return mod; |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 1925 | #undef ADD_TO_ALL |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 1926 | #undef SETBUILTIN |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1927 | } |