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