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