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