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