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