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