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