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