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" |
Georg Brandl | fc8eef3 | 2008-03-28 12:11:56 +0000 | [diff] [blame] | 4 | #include "Python-ast.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 5 | |
| 6 | #include "node.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 7 | #include "code.h" |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 8 | #include "eval.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 10 | #include <ctype.h> |
Mark Dickinson | bd15a06 | 2009-11-18 19:33:35 +0000 | [diff] [blame] | 11 | #include <float.h> /* for DBL_MANT_DIG and friends */ |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 13 | #ifdef RISCOS |
| 14 | #include "unixstuff.h" |
| 15 | #endif |
| 16 | |
Mark Hammond | 26cffde | 2001-05-14 12:17:34 +0000 | [diff] [blame] | 17 | /* The default encoding used by the platform file system APIs |
| 18 | Can remain NULL for all platforms that don't have such a concept |
| 19 | */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 20 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Mark Hammond | 26cffde | 2001-05-14 12:17:34 +0000 | [diff] [blame] | 21 | const char *Py_FileSystemDefaultEncoding = "mbcs"; |
Just van Rossum | b9b8e9c | 2003-02-10 09:22:01 +0000 | [diff] [blame] | 22 | #elif defined(__APPLE__) |
| 23 | const char *Py_FileSystemDefaultEncoding = "utf-8"; |
Mark Hammond | 26cffde | 2001-05-14 12:17:34 +0000 | [diff] [blame] | 24 | #else |
| 25 | const char *Py_FileSystemDefaultEncoding = NULL; /* use default */ |
| 26 | #endif |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 27 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 28 | /* Forward */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 29 | static PyObject *filterstring(PyObject *, PyObject *); |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 30 | #ifdef Py_USING_UNICODE |
| 31 | static PyObject *filterunicode(PyObject *, PyObject *); |
| 32 | #endif |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 33 | static PyObject *filtertuple (PyObject *, PyObject *); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 35 | static PyObject * |
Neal Norwitz | 92e212f | 2006-04-03 04:48:37 +0000 | [diff] [blame] | 36 | builtin___import__(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 37 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 38 | static char *kwlist[] = {"name", "globals", "locals", "fromlist", |
| 39 | "level", 0}; |
| 40 | char *name; |
| 41 | PyObject *globals = NULL; |
| 42 | PyObject *locals = NULL; |
| 43 | PyObject *fromlist = NULL; |
| 44 | int level = -1; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 45 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 46 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOi:__import__", |
| 47 | kwlist, &name, &globals, &locals, &fromlist, &level)) |
| 48 | return NULL; |
| 49 | return PyImport_ImportModuleLevel(name, globals, locals, |
| 50 | fromlist, level); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 53 | PyDoc_STRVAR(import_doc, |
Neal Norwitz | 92e212f | 2006-04-03 04:48:37 +0000 | [diff] [blame] | 54 | "__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 55 | \n\ |
R David Murray | 59488d2 | 2012-07-18 19:44:08 -0400 | [diff] [blame] | 56 | Import a module. Because this function is meant for use by the Python\n\ |
| 57 | interpreter and not for general use it is better to use\n\ |
| 58 | importlib.import_module() to programmatically import a module.\n\ |
| 59 | \n\ |
| 60 | The globals argument is only used to determine the context;\n\ |
| 61 | 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] | 62 | should be a list of names to emulate ``from name import ...'', or an\n\ |
| 63 | empty list to emulate ``import name''.\n\ |
| 64 | When importing a module from a package, note that __import__('A.B', ...)\n\ |
| 65 | returns package A when fromlist is empty, but its submodule B when\n\ |
Neal Norwitz | 92e212f | 2006-04-03 04:48:37 +0000 | [diff] [blame] | 66 | fromlist is not empty. Level is used to determine whether to perform \n\ |
| 67 | absolute or relative imports. -1 is the original strategy of attempting\n\ |
| 68 | both absolute and relative imports, 0 is absolute, a positive number\n\ |
| 69 | 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] | 70 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 71 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 72 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 73 | builtin_abs(PyObject *self, PyObject *v) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 74 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 75 | return PyNumber_Absolute(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 78 | PyDoc_STRVAR(abs_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 79 | "abs(number) -> number\n\ |
| 80 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 81 | Return the absolute value of the argument."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 82 | |
Raymond Hettinger | 96229b1 | 2005-03-11 06:49:40 +0000 | [diff] [blame] | 83 | static PyObject * |
| 84 | builtin_all(PyObject *self, PyObject *v) |
| 85 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 86 | PyObject *it, *item; |
| 87 | PyObject *(*iternext)(PyObject *); |
| 88 | int cmp; |
Raymond Hettinger | 96229b1 | 2005-03-11 06:49:40 +0000 | [diff] [blame] | 89 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 90 | it = PyObject_GetIter(v); |
| 91 | if (it == NULL) |
| 92 | return NULL; |
| 93 | iternext = *Py_TYPE(it)->tp_iternext; |
Raymond Hettinger | 96229b1 | 2005-03-11 06:49:40 +0000 | [diff] [blame] | 94 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 95 | for (;;) { |
| 96 | item = iternext(it); |
| 97 | if (item == NULL) |
| 98 | break; |
| 99 | cmp = PyObject_IsTrue(item); |
| 100 | Py_DECREF(item); |
| 101 | if (cmp < 0) { |
| 102 | Py_DECREF(it); |
| 103 | return NULL; |
| 104 | } |
| 105 | if (cmp == 0) { |
| 106 | Py_DECREF(it); |
| 107 | Py_RETURN_FALSE; |
| 108 | } |
| 109 | } |
| 110 | Py_DECREF(it); |
| 111 | if (PyErr_Occurred()) { |
| 112 | if (PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 113 | PyErr_Clear(); |
| 114 | else |
| 115 | return NULL; |
| 116 | } |
| 117 | Py_RETURN_TRUE; |
Raymond Hettinger | 96229b1 | 2005-03-11 06:49:40 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | PyDoc_STRVAR(all_doc, |
| 121 | "all(iterable) -> bool\n\ |
| 122 | \n\ |
Ezio Melotti | 94bf697 | 2013-02-15 23:35:14 +0200 | [diff] [blame] | 123 | Return True if bool(x) is True for all values x in the iterable.\n\ |
| 124 | If the iterable is empty, return True."); |
Raymond Hettinger | 96229b1 | 2005-03-11 06:49:40 +0000 | [diff] [blame] | 125 | |
| 126 | static PyObject * |
| 127 | builtin_any(PyObject *self, PyObject *v) |
| 128 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 129 | PyObject *it, *item; |
| 130 | PyObject *(*iternext)(PyObject *); |
| 131 | int cmp; |
Raymond Hettinger | 96229b1 | 2005-03-11 06:49:40 +0000 | [diff] [blame] | 132 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 133 | it = PyObject_GetIter(v); |
| 134 | if (it == NULL) |
| 135 | return NULL; |
| 136 | iternext = *Py_TYPE(it)->tp_iternext; |
Raymond Hettinger | 96229b1 | 2005-03-11 06:49:40 +0000 | [diff] [blame] | 137 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 138 | for (;;) { |
| 139 | item = iternext(it); |
| 140 | if (item == NULL) |
| 141 | break; |
| 142 | cmp = PyObject_IsTrue(item); |
| 143 | Py_DECREF(item); |
| 144 | if (cmp < 0) { |
| 145 | Py_DECREF(it); |
| 146 | return NULL; |
| 147 | } |
| 148 | if (cmp == 1) { |
| 149 | Py_DECREF(it); |
| 150 | Py_RETURN_TRUE; |
| 151 | } |
| 152 | } |
| 153 | Py_DECREF(it); |
| 154 | if (PyErr_Occurred()) { |
| 155 | if (PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 156 | PyErr_Clear(); |
| 157 | else |
| 158 | return NULL; |
| 159 | } |
| 160 | Py_RETURN_FALSE; |
Raymond Hettinger | 96229b1 | 2005-03-11 06:49:40 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | PyDoc_STRVAR(any_doc, |
| 164 | "any(iterable) -> bool\n\ |
| 165 | \n\ |
Ezio Melotti | 94bf697 | 2013-02-15 23:35:14 +0200 | [diff] [blame] | 166 | Return True if bool(x) is True for any x in the iterable.\n\ |
| 167 | If the iterable is empty, return False."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 168 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 169 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 170 | builtin_apply(PyObject *self, PyObject *args) |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 171 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 172 | PyObject *func, *alist = NULL, *kwdict = NULL; |
| 173 | PyObject *t = NULL, *retval = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 174 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 175 | if (PyErr_WarnPy3k("apply() not supported in 3.x; " |
| 176 | "use func(*args, **kwargs)", 1) < 0) |
| 177 | return NULL; |
Neal Norwitz | 8b2bfbc | 2007-05-23 06:35:32 +0000 | [diff] [blame] | 178 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 179 | if (!PyArg_UnpackTuple(args, "apply", 1, 3, &func, &alist, &kwdict)) |
| 180 | return NULL; |
| 181 | if (alist != NULL) { |
| 182 | if (!PyTuple_Check(alist)) { |
| 183 | if (!PySequence_Check(alist)) { |
| 184 | PyErr_Format(PyExc_TypeError, |
| 185 | "apply() arg 2 expected sequence, found %s", |
| 186 | alist->ob_type->tp_name); |
| 187 | return NULL; |
| 188 | } |
| 189 | t = PySequence_Tuple(alist); |
| 190 | if (t == NULL) |
| 191 | return NULL; |
| 192 | alist = t; |
| 193 | } |
| 194 | } |
| 195 | if (kwdict != NULL && !PyDict_Check(kwdict)) { |
| 196 | PyErr_Format(PyExc_TypeError, |
| 197 | "apply() arg 3 expected dictionary, found %s", |
| 198 | kwdict->ob_type->tp_name); |
| 199 | goto finally; |
| 200 | } |
| 201 | retval = PyEval_CallObjectWithKeywords(func, alist, kwdict); |
Barry Warsaw | 968f8cb | 1998-10-01 15:33:12 +0000 | [diff] [blame] | 202 | finally: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 203 | Py_XDECREF(t); |
| 204 | return retval; |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 207 | PyDoc_STRVAR(apply_doc, |
Fred Drake | f1fbc62 | 2001-01-12 17:05:05 +0000 | [diff] [blame] | 208 | "apply(object[, args[, kwargs]]) -> value\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 209 | \n\ |
Fred Drake | 7b91212 | 1999-12-23 14:16:55 +0000 | [diff] [blame] | 210 | Call a callable object with positional arguments taken from the tuple args,\n\ |
| 211 | and keyword arguments taken from the optional dictionary kwargs.\n\ |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 212 | Note that classes are callable, as are instances with a __call__() method.\n\ |
| 213 | \n\ |
| 214 | Deprecated since release 2.3. Instead, use the extended call syntax:\n\ |
| 215 | function(*args, **keywords)."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 216 | |
| 217 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 218 | static PyObject * |
Eric Smith | 3cd8194 | 2008-02-22 16:30:22 +0000 | [diff] [blame] | 219 | builtin_bin(PyObject *self, PyObject *v) |
| 220 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 221 | return PyNumber_ToBase(v, 2); |
Eric Smith | 3cd8194 | 2008-02-22 16:30:22 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | PyDoc_STRVAR(bin_doc, |
| 225 | "bin(number) -> string\n\ |
| 226 | \n\ |
| 227 | Return the binary representation of an integer or long integer."); |
| 228 | |
| 229 | |
| 230 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 231 | builtin_callable(PyObject *self, PyObject *v) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 232 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 233 | return PyBool_FromLong((long)PyCallable_Check(v)); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 236 | PyDoc_STRVAR(callable_doc, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 237 | "callable(object) -> bool\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 238 | \n\ |
| 239 | Return whether the object is callable (i.e., some kind of function).\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 240 | Note that classes are callable, as are instances with a __call__() method."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 241 | |
| 242 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 243 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 244 | builtin_filter(PyObject *self, PyObject *args) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 245 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 246 | PyObject *func, *seq, *result, *it, *arg; |
| 247 | Py_ssize_t len; /* guess for result list size */ |
| 248 | register Py_ssize_t j; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 249 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 250 | if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq)) |
| 251 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 252 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 253 | /* Strings and tuples return a result of the same type. */ |
| 254 | if (PyString_Check(seq)) |
| 255 | return filterstring(func, seq); |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 256 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 257 | if (PyUnicode_Check(seq)) |
| 258 | return filterunicode(func, seq); |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 259 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 260 | if (PyTuple_Check(seq)) |
| 261 | return filtertuple(func, seq); |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 262 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 263 | /* Pre-allocate argument list tuple. */ |
| 264 | arg = PyTuple_New(1); |
| 265 | if (arg == NULL) |
| 266 | return NULL; |
Georg Brandl | e35b657 | 2005-07-19 22:20:20 +0000 | [diff] [blame] | 267 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 268 | /* Get iterator. */ |
| 269 | it = PyObject_GetIter(seq); |
| 270 | if (it == NULL) |
| 271 | goto Fail_arg; |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 272 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 273 | /* Guess a result list size. */ |
| 274 | len = _PyObject_LengthHint(seq, 8); |
| 275 | if (len == -1) |
| 276 | goto Fail_it; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 277 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 278 | /* Get a result list. */ |
| 279 | if (PyList_Check(seq) && seq->ob_refcnt == 1) { |
| 280 | /* Eww - can modify the list in-place. */ |
| 281 | Py_INCREF(seq); |
| 282 | result = seq; |
| 283 | } |
| 284 | else { |
| 285 | result = PyList_New(len); |
| 286 | if (result == NULL) |
| 287 | goto Fail_it; |
| 288 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 289 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 290 | /* Build the result list. */ |
| 291 | j = 0; |
| 292 | for (;;) { |
| 293 | PyObject *item; |
| 294 | int ok; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 295 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 296 | item = PyIter_Next(it); |
| 297 | if (item == NULL) { |
| 298 | if (PyErr_Occurred()) |
| 299 | goto Fail_result_it; |
| 300 | break; |
| 301 | } |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 302 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 303 | if (func == (PyObject *)&PyBool_Type || func == Py_None) { |
| 304 | ok = PyObject_IsTrue(item); |
| 305 | } |
| 306 | else { |
| 307 | PyObject *good; |
| 308 | PyTuple_SET_ITEM(arg, 0, item); |
| 309 | good = PyObject_Call(func, arg, NULL); |
| 310 | PyTuple_SET_ITEM(arg, 0, NULL); |
| 311 | if (good == NULL) { |
| 312 | Py_DECREF(item); |
| 313 | goto Fail_result_it; |
| 314 | } |
| 315 | ok = PyObject_IsTrue(good); |
| 316 | Py_DECREF(good); |
| 317 | } |
Antoine Pitrou | c5bef75 | 2012-08-15 23:16:51 +0200 | [diff] [blame] | 318 | if (ok > 0) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 319 | if (j < len) |
| 320 | PyList_SET_ITEM(result, j, item); |
| 321 | else { |
| 322 | int status = PyList_Append(result, item); |
| 323 | Py_DECREF(item); |
| 324 | if (status < 0) |
| 325 | goto Fail_result_it; |
| 326 | } |
| 327 | ++j; |
| 328 | } |
Antoine Pitrou | c5bef75 | 2012-08-15 23:16:51 +0200 | [diff] [blame] | 329 | else { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 330 | Py_DECREF(item); |
Antoine Pitrou | c5bef75 | 2012-08-15 23:16:51 +0200 | [diff] [blame] | 331 | if (ok < 0) |
| 332 | goto Fail_result_it; |
| 333 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 334 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 335 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 336 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 337 | /* Cut back result list if len is too big. */ |
| 338 | if (j < len && PyList_SetSlice(result, j, len, NULL) < 0) |
| 339 | goto Fail_result_it; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 340 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 341 | Py_DECREF(it); |
| 342 | Py_DECREF(arg); |
| 343 | return result; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 344 | |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 345 | Fail_result_it: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 346 | Py_DECREF(result); |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 347 | Fail_it: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 348 | Py_DECREF(it); |
Guido van Rossum | c7903a1 | 2002-08-16 07:04:56 +0000 | [diff] [blame] | 349 | Fail_arg: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 350 | Py_DECREF(arg); |
| 351 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 354 | PyDoc_STRVAR(filter_doc, |
Tim Peters | d50e544 | 2002-03-09 00:06:26 +0000 | [diff] [blame] | 355 | "filter(function or None, sequence) -> list, tuple, or string\n" |
| 356 | "\n" |
| 357 | "Return those items of sequence for which function(item) is true. If\n" |
| 358 | "function is None, return the items that are true. If sequence is a tuple\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 359 | "or string, return the same type, else return a list."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 360 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 361 | static PyObject * |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 362 | builtin_format(PyObject *self, PyObject *args) |
| 363 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 364 | PyObject *value; |
| 365 | PyObject *format_spec = NULL; |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 366 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 367 | if (!PyArg_ParseTuple(args, "O|O:format", &value, &format_spec)) |
| 368 | return NULL; |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 369 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 370 | return PyObject_Format(value, format_spec); |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | PyDoc_STRVAR(format_doc, |
| 374 | "format(value[, format_spec]) -> string\n\ |
| 375 | \n\ |
| 376 | Returns value.__format__(format_spec)\n\ |
| 377 | format_spec defaults to \"\""); |
| 378 | |
| 379 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 380 | builtin_chr(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 381 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 382 | long x; |
| 383 | char s[1]; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 384 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 385 | if (!PyArg_ParseTuple(args, "l:chr", &x)) |
| 386 | return NULL; |
| 387 | if (x < 0 || x >= 256) { |
| 388 | PyErr_SetString(PyExc_ValueError, |
| 389 | "chr() arg not in range(256)"); |
| 390 | return NULL; |
| 391 | } |
| 392 | s[0] = (char)x; |
| 393 | return PyString_FromStringAndSize(s, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 396 | PyDoc_STRVAR(chr_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 397 | "chr(i) -> character\n\ |
| 398 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 399 | Return a string of one character with ordinal i; 0 <= i < 256."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 400 | |
| 401 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 402 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 403 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 404 | builtin_unichr(PyObject *self, PyObject *args) |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 405 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 406 | int x; |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 407 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 408 | if (!PyArg_ParseTuple(args, "i:unichr", &x)) |
| 409 | return NULL; |
Fredrik Lundh | 0dcf67e | 2001-06-26 20:01:56 +0000 | [diff] [blame] | 410 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 411 | return PyUnicode_FromOrdinal(x); |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 414 | PyDoc_STRVAR(unichr_doc, |
Fred Drake | 078b24f | 2000-04-13 02:42:50 +0000 | [diff] [blame] | 415 | "unichr(i) -> Unicode character\n\ |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 416 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 417 | Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff."); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 418 | #endif |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 419 | |
| 420 | |
| 421 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 422 | builtin_cmp(PyObject *self, PyObject *args) |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 423 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 424 | PyObject *a, *b; |
| 425 | int c; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 426 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 427 | if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b)) |
| 428 | return NULL; |
| 429 | if (PyObject_Cmp(a, b, &c) < 0) |
| 430 | return NULL; |
| 431 | return PyInt_FromLong((long)c); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 434 | PyDoc_STRVAR(cmp_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 435 | "cmp(x, y) -> integer\n\ |
| 436 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 437 | Return negative if x<y, zero if x==y, positive if x>y."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 438 | |
| 439 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 440 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 441 | builtin_coerce(PyObject *self, PyObject *args) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 442 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 443 | PyObject *v, *w; |
| 444 | PyObject *res; |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 445 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 446 | if (PyErr_WarnPy3k("coerce() not supported in 3.x", 1) < 0) |
| 447 | return NULL; |
Neal Norwitz | df25efe | 2007-05-23 06:58:36 +0000 | [diff] [blame] | 448 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 449 | if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w)) |
| 450 | return NULL; |
| 451 | if (PyNumber_Coerce(&v, &w) < 0) |
| 452 | return NULL; |
| 453 | res = PyTuple_Pack(2, v, w); |
| 454 | Py_DECREF(v); |
| 455 | Py_DECREF(w); |
| 456 | return res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 459 | PyDoc_STRVAR(coerce_doc, |
Martin v. Löwis | 8d494f3 | 2004-08-25 10:42:41 +0000 | [diff] [blame] | 460 | "coerce(x, y) -> (x1, y1)\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 461 | \n\ |
Martin v. Löwis | 8d494f3 | 2004-08-25 10:42:41 +0000 | [diff] [blame] | 462 | Return a tuple consisting of the two numeric arguments converted to\n\ |
| 463 | a common type, using the same rules as used by arithmetic operations.\n\ |
| 464 | If coercion is not possible, raise TypeError."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 465 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 466 | static PyObject * |
Georg Brandl | 5240d74 | 2007-03-13 20:46:32 +0000 | [diff] [blame] | 467 | builtin_compile(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 468 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 469 | char *str; |
| 470 | char *filename; |
| 471 | char *startstr; |
| 472 | int mode = -1; |
| 473 | int dont_inherit = 0; |
| 474 | int supplied_flags = 0; |
| 475 | int is_ast; |
| 476 | PyCompilerFlags cf; |
| 477 | PyObject *result = NULL, *cmd, *tmp = NULL; |
| 478 | Py_ssize_t length; |
| 479 | static char *kwlist[] = {"source", "filename", "mode", "flags", |
| 480 | "dont_inherit", NULL}; |
| 481 | int start[] = {Py_file_input, Py_eval_input, Py_single_input}; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 482 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 483 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile", |
| 484 | kwlist, &cmd, &filename, &startstr, |
| 485 | &supplied_flags, &dont_inherit)) |
| 486 | return NULL; |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 487 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 488 | cf.cf_flags = supplied_flags; |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 489 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 490 | if (supplied_flags & |
| 491 | ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST)) |
| 492 | { |
| 493 | PyErr_SetString(PyExc_ValueError, |
| 494 | "compile(): unrecognised flags"); |
| 495 | return NULL; |
| 496 | } |
| 497 | /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */ |
Georg Brandl | fc8eef3 | 2008-03-28 12:11:56 +0000 | [diff] [blame] | 498 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 499 | if (!dont_inherit) { |
| 500 | PyEval_MergeCompilerFlags(&cf); |
| 501 | } |
Georg Brandl | fc8eef3 | 2008-03-28 12:11:56 +0000 | [diff] [blame] | 502 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 503 | if (strcmp(startstr, "exec") == 0) |
| 504 | mode = 0; |
| 505 | else if (strcmp(startstr, "eval") == 0) |
| 506 | mode = 1; |
| 507 | else if (strcmp(startstr, "single") == 0) |
| 508 | mode = 2; |
| 509 | else { |
| 510 | PyErr_SetString(PyExc_ValueError, |
| 511 | "compile() arg 3 must be 'exec', 'eval' or 'single'"); |
| 512 | return NULL; |
| 513 | } |
Georg Brandl | f2bfd54 | 2008-03-29 13:24:23 +0000 | [diff] [blame] | 514 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 515 | is_ast = PyAST_Check(cmd); |
| 516 | if (is_ast == -1) |
| 517 | return NULL; |
| 518 | if (is_ast) { |
| 519 | if (supplied_flags & PyCF_ONLY_AST) { |
| 520 | Py_INCREF(cmd); |
| 521 | result = cmd; |
| 522 | } |
| 523 | else { |
| 524 | PyArena *arena; |
| 525 | mod_ty mod; |
Georg Brandl | fc8eef3 | 2008-03-28 12:11:56 +0000 | [diff] [blame] | 526 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 527 | arena = PyArena_New(); |
Stefan Krah | a8857af | 2012-08-20 17:31:22 +0200 | [diff] [blame] | 528 | if (arena == NULL) |
| 529 | return NULL; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 530 | mod = PyAST_obj2mod(cmd, arena, mode); |
| 531 | if (mod == NULL) { |
| 532 | PyArena_Free(arena); |
| 533 | return NULL; |
| 534 | } |
| 535 | result = (PyObject*)PyAST_Compile(mod, filename, |
| 536 | &cf, arena); |
| 537 | PyArena_Free(arena); |
| 538 | } |
| 539 | return result; |
| 540 | } |
Georg Brandl | fc8eef3 | 2008-03-28 12:11:56 +0000 | [diff] [blame] | 541 | |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 542 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 543 | if (PyUnicode_Check(cmd)) { |
| 544 | tmp = PyUnicode_AsUTF8String(cmd); |
| 545 | if (tmp == NULL) |
| 546 | return NULL; |
| 547 | cmd = tmp; |
| 548 | cf.cf_flags |= PyCF_SOURCE_IS_UTF8; |
| 549 | } |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 550 | #endif |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 551 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 552 | if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length)) |
| 553 | goto cleanup; |
| 554 | if ((size_t)length != strlen(str)) { |
| 555 | PyErr_SetString(PyExc_TypeError, |
| 556 | "compile() expected string without null bytes"); |
| 557 | goto cleanup; |
| 558 | } |
| 559 | result = Py_CompileStringFlags(str, filename, start[mode], &cf); |
Neal Norwitz | 3a9a3e7 | 2005-11-27 20:38:31 +0000 | [diff] [blame] | 560 | cleanup: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 561 | Py_XDECREF(tmp); |
| 562 | return result; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 565 | PyDoc_STRVAR(compile_doc, |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 566 | "compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 567 | \n\ |
| 568 | Compile the source string (a Python module, statement or expression)\n\ |
| 569 | into a code object that can be executed by the exec statement or eval().\n\ |
| 570 | The filename will be used for run-time error messages.\n\ |
| 571 | 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] | 572 | single (interactive) statement, or 'eval' to compile an expression.\n\ |
| 573 | The flags argument, if present, controls which future statements influence\n\ |
| 574 | the compilation of the code.\n\ |
| 575 | The dont_inherit argument, if non-zero, stops the compilation inheriting\n\ |
| 576 | the effects of any future statements in effect in the code calling\n\ |
| 577 | 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] | 578 | in addition to any features explicitly specified."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 579 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 580 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 581 | builtin_dir(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 582 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 583 | PyObject *arg = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 584 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 585 | if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg)) |
| 586 | return NULL; |
| 587 | return PyObject_Dir(arg); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 590 | PyDoc_STRVAR(dir_doc, |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 591 | "dir([object]) -> list of strings\n" |
| 592 | "\n" |
Georg Brandl | 871f1bc | 2007-03-12 13:17:36 +0000 | [diff] [blame] | 593 | "If called without an argument, return the names in the current scope.\n" |
| 594 | "Else, return an alphabetized list of names comprising (some of) the attributes\n" |
| 595 | "of the given object, and of attributes reachable from it.\n" |
| 596 | "If the object supplies a method named __dir__, it will be used; otherwise\n" |
| 597 | "the default dir() logic is used and returns:\n" |
| 598 | " for a module object: the module's attributes.\n" |
| 599 | " for a class object: its attributes, and recursively the attributes\n" |
| 600 | " of its bases.\n" |
Georg Brandl | 3bb1567 | 2007-03-13 07:23:16 +0000 | [diff] [blame] | 601 | " for any other object: its attributes, its class's attributes, and\n" |
Georg Brandl | 871f1bc | 2007-03-12 13:17:36 +0000 | [diff] [blame] | 602 | " recursively the attributes of its class's base classes."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 603 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 604 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 605 | builtin_divmod(PyObject *self, PyObject *args) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 606 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 607 | PyObject *v, *w; |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 608 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 609 | if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w)) |
| 610 | return NULL; |
| 611 | return PyNumber_Divmod(v, w); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 614 | PyDoc_STRVAR(divmod_doc, |
Raymond Hettinger | 39540a0 | 2011-07-19 11:59:20 -0700 | [diff] [blame] | 615 | "divmod(x, y) -> (quotient, remainder)\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 616 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 617 | 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] | 618 | |
| 619 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 620 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 621 | builtin_eval(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 622 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 623 | PyObject *cmd, *result, *tmp = NULL; |
| 624 | PyObject *globals = Py_None, *locals = Py_None; |
| 625 | char *str; |
| 626 | PyCompilerFlags cf; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 627 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 628 | if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals)) |
| 629 | return NULL; |
| 630 | if (locals != Py_None && !PyMapping_Check(locals)) { |
| 631 | PyErr_SetString(PyExc_TypeError, "locals must be a mapping"); |
| 632 | return NULL; |
| 633 | } |
| 634 | if (globals != Py_None && !PyDict_Check(globals)) { |
| 635 | PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ? |
| 636 | "globals must be a real dict; try eval(expr, {}, mapping)" |
| 637 | : "globals must be a dict"); |
| 638 | return NULL; |
| 639 | } |
| 640 | if (globals == Py_None) { |
| 641 | globals = PyEval_GetGlobals(); |
| 642 | if (locals == Py_None) |
| 643 | locals = PyEval_GetLocals(); |
| 644 | } |
| 645 | else if (locals == Py_None) |
| 646 | locals = globals; |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 647 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 648 | if (globals == NULL || locals == NULL) { |
| 649 | PyErr_SetString(PyExc_TypeError, |
| 650 | "eval must be given globals and locals " |
| 651 | "when called without a frame"); |
| 652 | return NULL; |
| 653 | } |
Georg Brandl | 77c85e6 | 2005-09-15 10:46:13 +0000 | [diff] [blame] | 654 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 655 | if (PyDict_GetItemString(globals, "__builtins__") == NULL) { |
| 656 | if (PyDict_SetItemString(globals, "__builtins__", |
| 657 | PyEval_GetBuiltins()) != 0) |
| 658 | return NULL; |
| 659 | } |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 660 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 661 | if (PyCode_Check(cmd)) { |
| 662 | if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) { |
| 663 | PyErr_SetString(PyExc_TypeError, |
| 664 | "code object passed to eval() may not contain free variables"); |
| 665 | return NULL; |
| 666 | } |
| 667 | return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals); |
| 668 | } |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 669 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 670 | if (!PyString_Check(cmd) && |
| 671 | !PyUnicode_Check(cmd)) { |
| 672 | PyErr_SetString(PyExc_TypeError, |
| 673 | "eval() arg 1 must be a string or code object"); |
| 674 | return NULL; |
| 675 | } |
| 676 | cf.cf_flags = 0; |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 677 | |
| 678 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 679 | if (PyUnicode_Check(cmd)) { |
| 680 | tmp = PyUnicode_AsUTF8String(cmd); |
| 681 | if (tmp == NULL) |
| 682 | return NULL; |
| 683 | cmd = tmp; |
| 684 | cf.cf_flags |= PyCF_SOURCE_IS_UTF8; |
| 685 | } |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 686 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 687 | if (PyString_AsStringAndSize(cmd, &str, NULL)) { |
| 688 | Py_XDECREF(tmp); |
| 689 | return NULL; |
| 690 | } |
| 691 | while (*str == ' ' || *str == '\t') |
| 692 | str++; |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 693 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 694 | (void)PyEval_MergeCompilerFlags(&cf); |
| 695 | result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf); |
| 696 | Py_XDECREF(tmp); |
| 697 | return result; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 700 | PyDoc_STRVAR(eval_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 701 | "eval(source[, globals[, locals]]) -> value\n\ |
| 702 | \n\ |
| 703 | Evaluate the source in the context of globals and locals.\n\ |
| 704 | The source may be a string representing a Python expression\n\ |
| 705 | or a code object as returned by compile().\n\ |
Neal Norwitz | 477ca1c | 2006-09-05 02:25:41 +0000 | [diff] [blame] | 706 | 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] | 707 | defaulting to the current globals and locals.\n\ |
| 708 | If only globals is given, locals defaults to it.\n"); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 709 | |
| 710 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 711 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 712 | builtin_execfile(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 713 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 714 | char *filename; |
| 715 | PyObject *globals = Py_None, *locals = Py_None; |
| 716 | PyObject *res; |
| 717 | FILE* fp = NULL; |
| 718 | PyCompilerFlags cf; |
| 719 | int exists; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 720 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 721 | if (PyErr_WarnPy3k("execfile() not supported in 3.x; use exec()", |
| 722 | 1) < 0) |
| 723 | return NULL; |
Neal Norwitz | df25efe | 2007-05-23 06:58:36 +0000 | [diff] [blame] | 724 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 725 | if (!PyArg_ParseTuple(args, "s|O!O:execfile", |
| 726 | &filename, |
| 727 | &PyDict_Type, &globals, |
| 728 | &locals)) |
| 729 | return NULL; |
| 730 | if (locals != Py_None && !PyMapping_Check(locals)) { |
| 731 | PyErr_SetString(PyExc_TypeError, "locals must be a mapping"); |
| 732 | return NULL; |
| 733 | } |
| 734 | if (globals == Py_None) { |
| 735 | globals = PyEval_GetGlobals(); |
| 736 | if (locals == Py_None) |
| 737 | locals = PyEval_GetLocals(); |
| 738 | } |
| 739 | else if (locals == Py_None) |
| 740 | locals = globals; |
| 741 | if (PyDict_GetItemString(globals, "__builtins__") == NULL) { |
| 742 | if (PyDict_SetItemString(globals, "__builtins__", |
| 743 | PyEval_GetBuiltins()) != 0) |
| 744 | return NULL; |
| 745 | } |
Martin v. Löwis | 6b3a2c4 | 2001-08-08 05:30:36 +0000 | [diff] [blame] | 746 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 747 | exists = 0; |
| 748 | /* Test for existence or directory. */ |
Martin v. Löwis | 3484a18 | 2002-03-09 12:07:51 +0000 | [diff] [blame] | 749 | #if defined(PLAN9) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 750 | { |
| 751 | Dir *d; |
Martin v. Löwis | 3484a18 | 2002-03-09 12:07:51 +0000 | [diff] [blame] | 752 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 753 | if ((d = dirstat(filename))!=nil) { |
| 754 | if(d->mode & DMDIR) |
| 755 | werrstr("is a directory"); |
| 756 | else |
| 757 | exists = 1; |
| 758 | free(d); |
| 759 | } |
| 760 | } |
Martin v. Löwis | 3484a18 | 2002-03-09 12:07:51 +0000 | [diff] [blame] | 761 | #elif defined(RISCOS) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 762 | if (object_exists(filename)) { |
| 763 | if (isdir(filename)) |
| 764 | errno = EISDIR; |
| 765 | else |
| 766 | exists = 1; |
| 767 | } |
| 768 | #else /* standard Posix */ |
| 769 | { |
| 770 | struct stat s; |
| 771 | if (stat(filename, &s) == 0) { |
| 772 | if (S_ISDIR(s.st_mode)) |
| 773 | # if defined(PYOS_OS2) && defined(PYCC_VACPP) |
| 774 | errno = EOS2ERR; |
| 775 | # else |
| 776 | errno = EISDIR; |
| 777 | # endif |
| 778 | else |
| 779 | exists = 1; |
| 780 | } |
| 781 | } |
Martin v. Löwis | 3484a18 | 2002-03-09 12:07:51 +0000 | [diff] [blame] | 782 | #endif |
Martin v. Löwis | 6b3a2c4 | 2001-08-08 05:30:36 +0000 | [diff] [blame] | 783 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 784 | if (exists) { |
| 785 | Py_BEGIN_ALLOW_THREADS |
| 786 | fp = fopen(filename, "r" PY_STDIOTEXTMODE); |
| 787 | Py_END_ALLOW_THREADS |
Martin v. Löwis | 6b3a2c4 | 2001-08-08 05:30:36 +0000 | [diff] [blame] | 788 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 789 | if (fp == NULL) { |
| 790 | exists = 0; |
Martin v. Löwis | 6b3a2c4 | 2001-08-08 05:30:36 +0000 | [diff] [blame] | 791 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 792 | } |
Martin v. Löwis | 6b3a2c4 | 2001-08-08 05:30:36 +0000 | [diff] [blame] | 793 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 794 | if (!exists) { |
| 795 | PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename); |
| 796 | return NULL; |
| 797 | } |
| 798 | cf.cf_flags = 0; |
| 799 | if (PyEval_MergeCompilerFlags(&cf)) |
| 800 | res = PyRun_FileExFlags(fp, filename, Py_file_input, globals, |
| 801 | locals, 1, &cf); |
| 802 | else |
| 803 | res = PyRun_FileEx(fp, filename, Py_file_input, globals, |
| 804 | locals, 1); |
| 805 | return res; |
Guido van Rossum | 0f61f8a | 1992-02-25 18:55:05 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 808 | PyDoc_STRVAR(execfile_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 809 | "execfile(filename[, globals[, locals]])\n\ |
| 810 | \n\ |
| 811 | Read and execute a Python script from a file.\n\ |
| 812 | The globals and locals are dictionaries, defaulting to the current\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 813 | globals and locals. If only globals is given, locals defaults to it."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 814 | |
| 815 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 816 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 817 | builtin_getattr(PyObject *self, PyObject *args) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 818 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 819 | PyObject *v, *result, *dflt = NULL; |
| 820 | PyObject *name; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 821 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 822 | if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt)) |
| 823 | return NULL; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 824 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 825 | if (PyUnicode_Check(name)) { |
| 826 | name = _PyUnicode_AsDefaultEncodedString(name, NULL); |
| 827 | if (name == NULL) |
| 828 | return NULL; |
| 829 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 830 | #endif |
Jeremy Hylton | 0eb1115 | 2001-07-30 22:39:31 +0000 | [diff] [blame] | 831 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 832 | if (!PyString_Check(name)) { |
| 833 | PyErr_SetString(PyExc_TypeError, |
| 834 | "getattr(): attribute name must be string"); |
| 835 | return NULL; |
| 836 | } |
| 837 | result = PyObject_GetAttr(v, name); |
| 838 | if (result == NULL && dflt != NULL && |
| 839 | PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 840 | { |
| 841 | PyErr_Clear(); |
| 842 | Py_INCREF(dflt); |
| 843 | result = dflt; |
| 844 | } |
| 845 | return result; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 848 | PyDoc_STRVAR(getattr_doc, |
Guido van Rossum | 950ff29 | 1998-06-29 13:38:57 +0000 | [diff] [blame] | 849 | "getattr(object, name[, default]) -> value\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 850 | \n\ |
Guido van Rossum | 950ff29 | 1998-06-29 13:38:57 +0000 | [diff] [blame] | 851 | Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\ |
| 852 | 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] | 853 | exist; without it, an exception is raised in that case."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 854 | |
| 855 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 856 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 857 | builtin_globals(PyObject *self) |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 858 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 859 | PyObject *d; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 860 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 861 | d = PyEval_GetGlobals(); |
| 862 | Py_XINCREF(d); |
| 863 | return d; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 866 | PyDoc_STRVAR(globals_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 867 | "globals() -> dictionary\n\ |
| 868 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 869 | Return the dictionary containing the current scope's global variables."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 870 | |
| 871 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 872 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 873 | builtin_hasattr(PyObject *self, PyObject *args) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 874 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 875 | PyObject *v; |
| 876 | PyObject *name; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 877 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 878 | if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name)) |
| 879 | return NULL; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 880 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 881 | if (PyUnicode_Check(name)) { |
| 882 | name = _PyUnicode_AsDefaultEncodedString(name, NULL); |
| 883 | if (name == NULL) |
| 884 | return NULL; |
| 885 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 886 | #endif |
Jeremy Hylton | 302b54a | 2001-07-30 22:45:19 +0000 | [diff] [blame] | 887 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 888 | if (!PyString_Check(name)) { |
| 889 | PyErr_SetString(PyExc_TypeError, |
| 890 | "hasattr(): attribute name must be string"); |
| 891 | return NULL; |
| 892 | } |
| 893 | v = PyObject_GetAttr(v, name); |
| 894 | if (v == NULL) { |
| 895 | if (!PyErr_ExceptionMatches(PyExc_Exception)) |
| 896 | return NULL; |
| 897 | else { |
| 898 | PyErr_Clear(); |
| 899 | Py_INCREF(Py_False); |
| 900 | return Py_False; |
| 901 | } |
| 902 | } |
| 903 | Py_DECREF(v); |
| 904 | Py_INCREF(Py_True); |
| 905 | return Py_True; |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 906 | } |
| 907 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 908 | PyDoc_STRVAR(hasattr_doc, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 909 | "hasattr(object, name) -> bool\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 910 | \n\ |
| 911 | Return whether the object has an attribute with the given name.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 912 | (This is done by calling getattr(object, name) and catching exceptions.)"); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 913 | |
| 914 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 915 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 916 | builtin_id(PyObject *self, PyObject *v) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 917 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 918 | return PyLong_FromVoidPtr(v); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 921 | PyDoc_STRVAR(id_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 922 | "id(object) -> integer\n\ |
| 923 | \n\ |
| 924 | 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] | 925 | simultaneously existing objects. (Hint: it's the object's memory address.)"); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 926 | |
| 927 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 928 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 929 | builtin_map(PyObject *self, PyObject *args) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 930 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 931 | typedef struct { |
| 932 | PyObject *it; /* the iterator object */ |
| 933 | int saw_StopIteration; /* bool: did the iterator end? */ |
| 934 | } sequence; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 935 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 936 | PyObject *func, *result; |
| 937 | sequence *seqs = NULL, *sqp; |
| 938 | Py_ssize_t n, len; |
| 939 | register int i, j; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 940 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 941 | n = PyTuple_Size(args); |
| 942 | if (n < 2) { |
| 943 | PyErr_SetString(PyExc_TypeError, |
| 944 | "map() requires at least two args"); |
| 945 | return NULL; |
| 946 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 947 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 948 | func = PyTuple_GetItem(args, 0); |
| 949 | n--; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 950 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 951 | if (func == Py_None) { |
| 952 | if (PyErr_WarnPy3k("map(None, ...) not supported in 3.x; " |
| 953 | "use list(...)", 1) < 0) |
| 954 | return NULL; |
| 955 | if (n == 1) { |
| 956 | /* map(None, S) is the same as list(S). */ |
| 957 | return PySequence_List(PyTuple_GetItem(args, 1)); |
| 958 | } |
| 959 | } |
Guido van Rossum | fa4ac71 | 1998-07-10 17:37:30 +0000 | [diff] [blame] | 960 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 961 | /* Get space for sequence descriptors. Must NULL out the iterator |
| 962 | * pointers so that jumping to Fail_2 later doesn't see trash. |
| 963 | */ |
| 964 | if ((seqs = PyMem_NEW(sequence, n)) == NULL) { |
| 965 | PyErr_NoMemory(); |
| 966 | return NULL; |
| 967 | } |
| 968 | for (i = 0; i < n; ++i) { |
| 969 | seqs[i].it = (PyObject*)NULL; |
| 970 | seqs[i].saw_StopIteration = 0; |
| 971 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 972 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 973 | /* Do a first pass to obtain iterators for the arguments, and set len |
| 974 | * to the largest of their lengths. |
| 975 | */ |
| 976 | len = 0; |
| 977 | for (i = 0, sqp = seqs; i < n; ++i, ++sqp) { |
| 978 | PyObject *curseq; |
| 979 | Py_ssize_t curlen; |
Guido van Rossum | ad99177 | 2001-01-12 16:03:05 +0000 | [diff] [blame] | 980 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 981 | /* Get iterator. */ |
| 982 | curseq = PyTuple_GetItem(args, i+1); |
| 983 | sqp->it = PyObject_GetIter(curseq); |
| 984 | if (sqp->it == NULL) { |
| 985 | static char errmsg[] = |
| 986 | "argument %d to map() must support iteration"; |
| 987 | char errbuf[sizeof(errmsg) + 25]; |
| 988 | PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2); |
| 989 | PyErr_SetString(PyExc_TypeError, errbuf); |
| 990 | goto Fail_2; |
| 991 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 992 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 993 | /* Update len. */ |
| 994 | curlen = _PyObject_LengthHint(curseq, 8); |
| 995 | if (curlen > len) |
| 996 | len = curlen; |
| 997 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 998 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 999 | /* Get space for the result list. */ |
| 1000 | if ((result = (PyObject *) PyList_New(len)) == NULL) |
| 1001 | goto Fail_2; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1002 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1003 | /* Iterate over the sequences until all have stopped. */ |
| 1004 | for (i = 0; ; ++i) { |
| 1005 | PyObject *alist, *item=NULL, *value; |
| 1006 | int numactive = 0; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1007 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1008 | if (func == Py_None && n == 1) |
| 1009 | alist = NULL; |
| 1010 | else if ((alist = PyTuple_New(n)) == NULL) |
| 1011 | goto Fail_1; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1012 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1013 | for (j = 0, sqp = seqs; j < n; ++j, ++sqp) { |
| 1014 | if (sqp->saw_StopIteration) { |
| 1015 | Py_INCREF(Py_None); |
| 1016 | item = Py_None; |
| 1017 | } |
| 1018 | else { |
| 1019 | item = PyIter_Next(sqp->it); |
| 1020 | if (item) |
| 1021 | ++numactive; |
| 1022 | else { |
| 1023 | if (PyErr_Occurred()) { |
| 1024 | Py_XDECREF(alist); |
| 1025 | goto Fail_1; |
| 1026 | } |
| 1027 | Py_INCREF(Py_None); |
| 1028 | item = Py_None; |
| 1029 | sqp->saw_StopIteration = 1; |
| 1030 | } |
| 1031 | } |
| 1032 | if (alist) |
| 1033 | PyTuple_SET_ITEM(alist, j, item); |
| 1034 | else |
| 1035 | break; |
| 1036 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1037 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1038 | if (!alist) |
| 1039 | alist = item; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1040 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1041 | if (numactive == 0) { |
| 1042 | Py_DECREF(alist); |
| 1043 | break; |
| 1044 | } |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1045 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1046 | if (func == Py_None) |
| 1047 | value = alist; |
| 1048 | else { |
| 1049 | value = PyEval_CallObject(func, alist); |
| 1050 | Py_DECREF(alist); |
| 1051 | if (value == NULL) |
| 1052 | goto Fail_1; |
| 1053 | } |
| 1054 | if (i >= len) { |
| 1055 | int status = PyList_Append(result, value); |
| 1056 | Py_DECREF(value); |
| 1057 | if (status < 0) |
| 1058 | goto Fail_1; |
| 1059 | } |
| 1060 | else if (PyList_SetItem(result, i, value) < 0) |
| 1061 | goto Fail_1; |
| 1062 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1063 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1064 | if (i < len && PyList_SetSlice(result, i, len, NULL) < 0) |
| 1065 | goto Fail_1; |
Guido van Rossum | fa4ac71 | 1998-07-10 17:37:30 +0000 | [diff] [blame] | 1066 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1067 | goto Succeed; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1068 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1069 | Fail_1: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1070 | Py_DECREF(result); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1071 | Fail_2: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1072 | result = NULL; |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 1073 | Succeed: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1074 | assert(seqs); |
| 1075 | for (i = 0; i < n; ++i) |
| 1076 | Py_XDECREF(seqs[i].it); |
| 1077 | PyMem_DEL(seqs); |
| 1078 | return result; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1081 | PyDoc_STRVAR(map_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1082 | "map(function, sequence[, sequence, ...]) -> list\n\ |
| 1083 | \n\ |
| 1084 | Return a list of the results of applying the function to the items of\n\ |
| 1085 | the argument sequence(s). If more than one sequence is given, the\n\ |
| 1086 | function is called with an argument list consisting of the corresponding\n\ |
| 1087 | item of each sequence, substituting None for missing values when not all\n\ |
| 1088 | sequences have the same length. If the function is None, return a list of\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1089 | the items of the sequence (or a list of tuples if more than one sequence)."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1090 | |
| 1091 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1092 | static PyObject * |
Georg Brandl | 28e0873 | 2008-04-30 19:47:09 +0000 | [diff] [blame] | 1093 | builtin_next(PyObject *self, PyObject *args) |
| 1094 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1095 | PyObject *it, *res; |
| 1096 | PyObject *def = NULL; |
Georg Brandl | 28e0873 | 2008-04-30 19:47:09 +0000 | [diff] [blame] | 1097 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1098 | if (!PyArg_UnpackTuple(args, "next", 1, 2, &it, &def)) |
| 1099 | return NULL; |
| 1100 | if (!PyIter_Check(it)) { |
| 1101 | PyErr_Format(PyExc_TypeError, |
| 1102 | "%.200s object is not an iterator", |
| 1103 | it->ob_type->tp_name); |
| 1104 | return NULL; |
| 1105 | } |
| 1106 | |
| 1107 | res = (*it->ob_type->tp_iternext)(it); |
| 1108 | if (res != NULL) { |
| 1109 | return res; |
| 1110 | } else if (def != NULL) { |
| 1111 | if (PyErr_Occurred()) { |
| 1112 | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 1113 | return NULL; |
| 1114 | PyErr_Clear(); |
| 1115 | } |
| 1116 | Py_INCREF(def); |
| 1117 | return def; |
| 1118 | } else if (PyErr_Occurred()) { |
| 1119 | return NULL; |
| 1120 | } else { |
| 1121 | PyErr_SetNone(PyExc_StopIteration); |
| 1122 | return NULL; |
| 1123 | } |
Georg Brandl | 28e0873 | 2008-04-30 19:47:09 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | PyDoc_STRVAR(next_doc, |
| 1127 | "next(iterator[, default])\n\ |
| 1128 | \n\ |
| 1129 | Return the next item from the iterator. If default is given and the iterator\n\ |
| 1130 | is exhausted, it is returned instead of raising StopIteration."); |
| 1131 | |
| 1132 | |
| 1133 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1134 | builtin_setattr(PyObject *self, PyObject *args) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 1135 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1136 | PyObject *v; |
| 1137 | PyObject *name; |
| 1138 | PyObject *value; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1139 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1140 | if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value)) |
| 1141 | return NULL; |
| 1142 | if (PyObject_SetAttr(v, name, value) != 0) |
| 1143 | return NULL; |
| 1144 | Py_INCREF(Py_None); |
| 1145 | return Py_None; |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1148 | PyDoc_STRVAR(setattr_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1149 | "setattr(object, name, value)\n\ |
| 1150 | \n\ |
| 1151 | 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] | 1152 | ``x.y = v''."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1153 | |
| 1154 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1155 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1156 | builtin_delattr(PyObject *self, PyObject *args) |
Guido van Rossum | 14144fc | 1994-08-29 12:53:40 +0000 | [diff] [blame] | 1157 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1158 | PyObject *v; |
| 1159 | PyObject *name; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1160 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1161 | if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name)) |
| 1162 | return NULL; |
| 1163 | if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0) |
| 1164 | return NULL; |
| 1165 | Py_INCREF(Py_None); |
| 1166 | return Py_None; |
Guido van Rossum | 14144fc | 1994-08-29 12:53:40 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1169 | PyDoc_STRVAR(delattr_doc, |
Guido van Rossum | df12a59 | 1998-11-23 22:13:04 +0000 | [diff] [blame] | 1170 | "delattr(object, name)\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1171 | \n\ |
| 1172 | 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] | 1173 | ``del x.y''."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1174 | |
| 1175 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1176 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1177 | builtin_hash(PyObject *self, PyObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1178 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1179 | long x; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1180 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1181 | x = PyObject_Hash(v); |
| 1182 | if (x == -1) |
| 1183 | return NULL; |
| 1184 | return PyInt_FromLong(x); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1187 | PyDoc_STRVAR(hash_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1188 | "hash(object) -> integer\n\ |
| 1189 | \n\ |
| 1190 | 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] | 1191 | 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] | 1192 | |
| 1193 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1194 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1195 | builtin_hex(PyObject *self, PyObject *v) |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 1196 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1197 | PyNumberMethods *nb; |
| 1198 | PyObject *res; |
Benjamin Peterson | c6d64ec | 2008-05-17 20:09:42 +0000 | [diff] [blame] | 1199 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1200 | if ((nb = v->ob_type->tp_as_number) == NULL || |
| 1201 | nb->nb_hex == NULL) { |
| 1202 | PyErr_SetString(PyExc_TypeError, |
| 1203 | "hex() argument can't be converted to hex"); |
| 1204 | return NULL; |
| 1205 | } |
| 1206 | res = (*nb->nb_hex)(v); |
| 1207 | if (res && !PyString_Check(res)) { |
| 1208 | PyErr_Format(PyExc_TypeError, |
| 1209 | "__hex__ returned non-string (type %.200s)", |
| 1210 | res->ob_type->tp_name); |
| 1211 | Py_DECREF(res); |
| 1212 | return NULL; |
| 1213 | } |
| 1214 | return res; |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1217 | PyDoc_STRVAR(hex_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1218 | "hex(number) -> string\n\ |
| 1219 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1220 | Return the hexadecimal representation of an integer or long integer."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1221 | |
| 1222 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 1223 | static PyObject *builtin_raw_input(PyObject *, PyObject *); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1224 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1225 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1226 | builtin_input(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1227 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1228 | PyObject *line; |
| 1229 | char *str; |
| 1230 | PyObject *res; |
| 1231 | PyObject *globals, *locals; |
| 1232 | PyCompilerFlags cf; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1233 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1234 | line = builtin_raw_input(self, args); |
| 1235 | if (line == NULL) |
| 1236 | return line; |
| 1237 | if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str)) |
| 1238 | return NULL; |
| 1239 | while (*str == ' ' || *str == '\t') |
| 1240 | str++; |
| 1241 | globals = PyEval_GetGlobals(); |
| 1242 | locals = PyEval_GetLocals(); |
| 1243 | if (PyDict_GetItemString(globals, "__builtins__") == NULL) { |
| 1244 | if (PyDict_SetItemString(globals, "__builtins__", |
| 1245 | PyEval_GetBuiltins()) != 0) |
| 1246 | return NULL; |
| 1247 | } |
| 1248 | cf.cf_flags = 0; |
| 1249 | PyEval_MergeCompilerFlags(&cf); |
| 1250 | res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf); |
| 1251 | Py_DECREF(line); |
| 1252 | return res; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1255 | PyDoc_STRVAR(input_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1256 | "input([prompt]) -> value\n\ |
| 1257 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1258 | Equivalent to eval(raw_input(prompt))."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1259 | |
| 1260 | |
Guido van Rossum | e8811f8 | 1997-02-14 15:48:05 +0000 | [diff] [blame] | 1261 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1262 | builtin_intern(PyObject *self, PyObject *args) |
Guido van Rossum | e8811f8 | 1997-02-14 15:48:05 +0000 | [diff] [blame] | 1263 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1264 | PyObject *s; |
| 1265 | if (!PyArg_ParseTuple(args, "S:intern", &s)) |
| 1266 | return NULL; |
| 1267 | if (!PyString_CheckExact(s)) { |
| 1268 | PyErr_SetString(PyExc_TypeError, |
| 1269 | "can't intern subclass of string"); |
| 1270 | return NULL; |
| 1271 | } |
| 1272 | Py_INCREF(s); |
| 1273 | PyString_InternInPlace(&s); |
| 1274 | return s; |
Guido van Rossum | e8811f8 | 1997-02-14 15:48:05 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1277 | PyDoc_STRVAR(intern_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1278 | "intern(string) -> string\n\ |
| 1279 | \n\ |
| 1280 | ``Intern'' the given string. This enters the string in the (global)\n\ |
| 1281 | table of interned strings whose purpose is to speed up dictionary lookups.\n\ |
| 1282 | Return the string itself or the previously interned string object with the\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1283 | same value."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1284 | |
| 1285 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1286 | static PyObject * |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1287 | builtin_iter(PyObject *self, PyObject *args) |
| 1288 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1289 | PyObject *v, *w = NULL; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1290 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1291 | if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w)) |
| 1292 | return NULL; |
| 1293 | if (w == NULL) |
| 1294 | return PyObject_GetIter(v); |
| 1295 | if (!PyCallable_Check(v)) { |
| 1296 | PyErr_SetString(PyExc_TypeError, |
| 1297 | "iter(v, w): v must be callable"); |
| 1298 | return NULL; |
| 1299 | } |
| 1300 | return PyCallIter_New(v, w); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1303 | PyDoc_STRVAR(iter_doc, |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1304 | "iter(collection) -> iterator\n\ |
| 1305 | iter(callable, sentinel) -> iterator\n\ |
| 1306 | \n\ |
| 1307 | Get an iterator from an object. In the first form, the argument must\n\ |
| 1308 | supply its own iterator, or be a sequence.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1309 | 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] | 1310 | |
| 1311 | |
| 1312 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1313 | builtin_len(PyObject *self, PyObject *v) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1314 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1315 | Py_ssize_t res; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1316 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1317 | res = PyObject_Size(v); |
| 1318 | if (res < 0 && PyErr_Occurred()) |
| 1319 | return NULL; |
| 1320 | return PyInt_FromSsize_t(res); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1323 | PyDoc_STRVAR(len_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1324 | "len(object) -> integer\n\ |
| 1325 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1326 | Return the number of items of a sequence or mapping."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1327 | |
| 1328 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1329 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1330 | builtin_locals(PyObject *self) |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1331 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1332 | PyObject *d; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1333 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1334 | d = PyEval_GetLocals(); |
| 1335 | Py_XINCREF(d); |
| 1336 | return d; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1339 | PyDoc_STRVAR(locals_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1340 | "locals() -> dictionary\n\ |
| 1341 | \n\ |
Raymond Hettinger | 69bf8f3 | 2003-01-04 02:16:22 +0000 | [diff] [blame] | 1342 | 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] | 1343 | |
| 1344 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1345 | static PyObject * |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1346 | min_max(PyObject *args, PyObject *kwds, int op) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1347 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1348 | PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL; |
| 1349 | const char *name = op == Py_LT ? "min" : "max"; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1350 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1351 | if (PyTuple_Size(args) > 1) |
| 1352 | v = args; |
| 1353 | else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v)) |
| 1354 | return NULL; |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1355 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1356 | if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) { |
| 1357 | keyfunc = PyDict_GetItemString(kwds, "key"); |
| 1358 | if (PyDict_Size(kwds)!=1 || keyfunc == NULL) { |
| 1359 | PyErr_Format(PyExc_TypeError, |
| 1360 | "%s() got an unexpected keyword argument", name); |
| 1361 | return NULL; |
| 1362 | } |
| 1363 | Py_INCREF(keyfunc); |
| 1364 | } |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1365 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1366 | it = PyObject_GetIter(v); |
| 1367 | if (it == NULL) { |
| 1368 | Py_XDECREF(keyfunc); |
| 1369 | return NULL; |
| 1370 | } |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 1371 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1372 | maxitem = NULL; /* the result */ |
| 1373 | maxval = NULL; /* the value associated with the result */ |
| 1374 | while (( item = PyIter_Next(it) )) { |
| 1375 | /* get the value from the key function */ |
| 1376 | if (keyfunc != NULL) { |
| 1377 | val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL); |
| 1378 | if (val == NULL) |
| 1379 | goto Fail_it_item; |
| 1380 | } |
| 1381 | /* no key function; the value is the item */ |
| 1382 | else { |
| 1383 | val = item; |
| 1384 | Py_INCREF(val); |
| 1385 | } |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 1386 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1387 | /* maximum value and item are unset; set them */ |
| 1388 | if (maxval == NULL) { |
| 1389 | maxitem = item; |
| 1390 | maxval = val; |
| 1391 | } |
| 1392 | /* maximum value and item are set; update them as necessary */ |
| 1393 | else { |
| 1394 | int cmp = PyObject_RichCompareBool(val, maxval, op); |
| 1395 | if (cmp < 0) |
| 1396 | goto Fail_it_item_and_val; |
| 1397 | else if (cmp > 0) { |
| 1398 | Py_DECREF(maxval); |
| 1399 | Py_DECREF(maxitem); |
| 1400 | maxval = val; |
| 1401 | maxitem = item; |
| 1402 | } |
| 1403 | else { |
| 1404 | Py_DECREF(item); |
| 1405 | Py_DECREF(val); |
| 1406 | } |
| 1407 | } |
| 1408 | } |
| 1409 | if (PyErr_Occurred()) |
| 1410 | goto Fail_it; |
| 1411 | if (maxval == NULL) { |
| 1412 | PyErr_Format(PyExc_ValueError, |
| 1413 | "%s() arg is an empty sequence", name); |
| 1414 | assert(maxitem == NULL); |
| 1415 | } |
| 1416 | else |
| 1417 | Py_DECREF(maxval); |
| 1418 | Py_DECREF(it); |
| 1419 | Py_XDECREF(keyfunc); |
| 1420 | return maxitem; |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1421 | |
| 1422 | Fail_it_item_and_val: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1423 | Py_DECREF(val); |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1424 | Fail_it_item: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1425 | Py_DECREF(item); |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1426 | Fail_it: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1427 | Py_XDECREF(maxval); |
| 1428 | Py_XDECREF(maxitem); |
| 1429 | Py_DECREF(it); |
| 1430 | Py_XDECREF(keyfunc); |
| 1431 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1434 | static PyObject * |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1435 | builtin_min(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1436 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1437 | return min_max(args, kwds, Py_LT); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1440 | PyDoc_STRVAR(min_doc, |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1441 | "min(iterable[, key=func]) -> value\n\ |
| 1442 | min(a, b, c, ...[, key=func]) -> value\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1443 | \n\ |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1444 | With a single iterable argument, return its smallest item.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1445 | With two or more arguments, return the smallest argument."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1446 | |
| 1447 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1448 | static PyObject * |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1449 | builtin_max(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1450 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1451 | return min_max(args, kwds, Py_GT); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1454 | PyDoc_STRVAR(max_doc, |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1455 | "max(iterable[, key=func]) -> value\n\ |
| 1456 | max(a, b, c, ...[, key=func]) -> value\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1457 | \n\ |
Raymond Hettinger | 3b0c7c2 | 2004-12-03 08:30:39 +0000 | [diff] [blame] | 1458 | With a single iterable argument, return its largest item.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1459 | With two or more arguments, return the largest argument."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1460 | |
| 1461 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1462 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1463 | builtin_oct(PyObject *self, PyObject *v) |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 1464 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1465 | PyNumberMethods *nb; |
| 1466 | PyObject *res; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1467 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1468 | if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL || |
| 1469 | nb->nb_oct == NULL) { |
| 1470 | PyErr_SetString(PyExc_TypeError, |
| 1471 | "oct() argument can't be converted to oct"); |
| 1472 | return NULL; |
| 1473 | } |
| 1474 | res = (*nb->nb_oct)(v); |
| 1475 | if (res && !PyString_Check(res)) { |
| 1476 | PyErr_Format(PyExc_TypeError, |
| 1477 | "__oct__ returned non-string (type %.200s)", |
| 1478 | res->ob_type->tp_name); |
| 1479 | Py_DECREF(res); |
| 1480 | return NULL; |
| 1481 | } |
| 1482 | return res; |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1485 | PyDoc_STRVAR(oct_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1486 | "oct(number) -> string\n\ |
| 1487 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1488 | Return the octal representation of an integer or long integer."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1489 | |
| 1490 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1491 | static PyObject * |
Neal Norwitz | c4edb0e | 2006-05-02 04:43:14 +0000 | [diff] [blame] | 1492 | builtin_open(PyObject *self, PyObject *args, PyObject *kwds) |
| 1493 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1494 | return PyObject_Call((PyObject*)&PyFile_Type, args, kwds); |
Neal Norwitz | c4edb0e | 2006-05-02 04:43:14 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
| 1497 | PyDoc_STRVAR(open_doc, |
| 1498 | "open(name[, mode[, buffering]]) -> file object\n\ |
| 1499 | \n\ |
Skip Montanaro | 4e3ebe0 | 2007-12-08 14:37:43 +0000 | [diff] [blame] | 1500 | Open a file using the file() type, returns a file object. This is the\n\ |
Philip Jenvey | dd0388a | 2009-05-28 03:12:16 +0000 | [diff] [blame] | 1501 | preferred way to open a file. See file.__doc__ for further information."); |
Neal Norwitz | c4edb0e | 2006-05-02 04:43:14 +0000 | [diff] [blame] | 1502 | |
| 1503 | |
| 1504 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1505 | builtin_ord(PyObject *self, PyObject* obj) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1506 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1507 | long ord; |
| 1508 | Py_ssize_t size; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1509 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1510 | if (PyString_Check(obj)) { |
| 1511 | size = PyString_GET_SIZE(obj); |
| 1512 | if (size == 1) { |
| 1513 | ord = (long)((unsigned char)*PyString_AS_STRING(obj)); |
| 1514 | return PyInt_FromLong(ord); |
| 1515 | } |
| 1516 | } else if (PyByteArray_Check(obj)) { |
| 1517 | size = PyByteArray_GET_SIZE(obj); |
| 1518 | if (size == 1) { |
| 1519 | ord = (long)((unsigned char)*PyByteArray_AS_STRING(obj)); |
| 1520 | return PyInt_FromLong(ord); |
| 1521 | } |
Christian Heimes | 1a6387e | 2008-03-26 12:49:49 +0000 | [diff] [blame] | 1522 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1523 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1524 | } else if (PyUnicode_Check(obj)) { |
| 1525 | size = PyUnicode_GET_SIZE(obj); |
| 1526 | if (size == 1) { |
| 1527 | ord = (long)*PyUnicode_AS_UNICODE(obj); |
| 1528 | return PyInt_FromLong(ord); |
| 1529 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1530 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1531 | } else { |
| 1532 | PyErr_Format(PyExc_TypeError, |
| 1533 | "ord() expected string of length 1, but " \ |
| 1534 | "%.200s found", obj->ob_type->tp_name); |
| 1535 | return NULL; |
| 1536 | } |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 1537 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1538 | PyErr_Format(PyExc_TypeError, |
| 1539 | "ord() expected a character, " |
| 1540 | "but string of length %zd found", |
| 1541 | size); |
| 1542 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1545 | PyDoc_STRVAR(ord_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1546 | "ord(c) -> integer\n\ |
| 1547 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1548 | Return the integer ordinal of a one-character string."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1549 | |
| 1550 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1551 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1552 | builtin_pow(PyObject *self, PyObject *args) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1553 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1554 | PyObject *v, *w, *z = Py_None; |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1555 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1556 | if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z)) |
| 1557 | return NULL; |
| 1558 | return PyNumber_Power(v, w, z); |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1561 | PyDoc_STRVAR(pow_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1562 | "pow(x, y[, z]) -> number\n\ |
| 1563 | \n\ |
| 1564 | 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] | 1565 | 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] | 1566 | |
| 1567 | |
Eric Smith | 7c47894 | 2008-03-18 23:45:49 +0000 | [diff] [blame] | 1568 | static PyObject * |
| 1569 | builtin_print(PyObject *self, PyObject *args, PyObject *kwds) |
| 1570 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1571 | static char *kwlist[] = {"sep", "end", "file", 0}; |
| 1572 | static PyObject *dummy_args = NULL; |
| 1573 | static PyObject *unicode_newline = NULL, *unicode_space = NULL; |
| 1574 | static PyObject *str_newline = NULL, *str_space = NULL; |
| 1575 | PyObject *newline, *space; |
| 1576 | PyObject *sep = NULL, *end = NULL, *file = NULL; |
| 1577 | int i, err, use_unicode = 0; |
Eric Smith | 7c47894 | 2008-03-18 23:45:49 +0000 | [diff] [blame] | 1578 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1579 | if (dummy_args == NULL) { |
| 1580 | if (!(dummy_args = PyTuple_New(0))) |
| 1581 | return NULL; |
| 1582 | } |
| 1583 | if (str_newline == NULL) { |
| 1584 | str_newline = PyString_FromString("\n"); |
| 1585 | if (str_newline == NULL) |
| 1586 | return NULL; |
| 1587 | str_space = PyString_FromString(" "); |
| 1588 | if (str_space == NULL) { |
| 1589 | Py_CLEAR(str_newline); |
| 1590 | return NULL; |
| 1591 | } |
Martin v. Löwis | ed11a5d | 2012-05-20 10:42:17 +0200 | [diff] [blame] | 1592 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1593 | unicode_newline = PyUnicode_FromString("\n"); |
| 1594 | if (unicode_newline == NULL) { |
| 1595 | Py_CLEAR(str_newline); |
| 1596 | Py_CLEAR(str_space); |
| 1597 | return NULL; |
| 1598 | } |
| 1599 | unicode_space = PyUnicode_FromString(" "); |
| 1600 | if (unicode_space == NULL) { |
| 1601 | Py_CLEAR(str_newline); |
| 1602 | Py_CLEAR(str_space); |
| 1603 | Py_CLEAR(unicode_space); |
| 1604 | return NULL; |
| 1605 | } |
Martin v. Löwis | ed11a5d | 2012-05-20 10:42:17 +0200 | [diff] [blame] | 1606 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1607 | } |
| 1608 | if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print", |
| 1609 | kwlist, &sep, &end, &file)) |
| 1610 | return NULL; |
| 1611 | if (file == NULL || file == Py_None) { |
| 1612 | file = PySys_GetObject("stdout"); |
| 1613 | /* sys.stdout may be None when FILE* stdout isn't connected */ |
| 1614 | if (file == Py_None) |
| 1615 | Py_RETURN_NONE; |
| 1616 | } |
| 1617 | if (sep == Py_None) { |
| 1618 | sep = NULL; |
| 1619 | } |
| 1620 | else if (sep) { |
| 1621 | if (PyUnicode_Check(sep)) { |
| 1622 | use_unicode = 1; |
| 1623 | } |
| 1624 | else if (!PyString_Check(sep)) { |
| 1625 | PyErr_Format(PyExc_TypeError, |
| 1626 | "sep must be None, str or unicode, not %.200s", |
| 1627 | sep->ob_type->tp_name); |
| 1628 | return NULL; |
| 1629 | } |
| 1630 | } |
| 1631 | if (end == Py_None) |
| 1632 | end = NULL; |
| 1633 | else if (end) { |
| 1634 | if (PyUnicode_Check(end)) { |
| 1635 | use_unicode = 1; |
| 1636 | } |
| 1637 | else if (!PyString_Check(end)) { |
| 1638 | PyErr_Format(PyExc_TypeError, |
| 1639 | "end must be None, str or unicode, not %.200s", |
| 1640 | end->ob_type->tp_name); |
| 1641 | return NULL; |
| 1642 | } |
| 1643 | } |
Benjamin Peterson | 753d162 | 2009-07-02 18:16:45 +0000 | [diff] [blame] | 1644 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1645 | if (!use_unicode) { |
| 1646 | for (i = 0; i < PyTuple_Size(args); i++) { |
| 1647 | if (PyUnicode_Check(PyTuple_GET_ITEM(args, i))) { |
| 1648 | use_unicode = 1; |
| 1649 | break; |
| 1650 | } |
| 1651 | } |
| 1652 | } |
| 1653 | if (use_unicode) { |
| 1654 | newline = unicode_newline; |
| 1655 | space = unicode_space; |
| 1656 | } |
| 1657 | else { |
| 1658 | newline = str_newline; |
| 1659 | space = str_space; |
| 1660 | } |
Eric Smith | 7c47894 | 2008-03-18 23:45:49 +0000 | [diff] [blame] | 1661 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1662 | for (i = 0; i < PyTuple_Size(args); i++) { |
| 1663 | if (i > 0) { |
| 1664 | if (sep == NULL) |
| 1665 | err = PyFile_WriteObject(space, file, |
| 1666 | Py_PRINT_RAW); |
| 1667 | else |
| 1668 | err = PyFile_WriteObject(sep, file, |
| 1669 | Py_PRINT_RAW); |
| 1670 | if (err) |
| 1671 | return NULL; |
| 1672 | } |
| 1673 | err = PyFile_WriteObject(PyTuple_GetItem(args, i), file, |
| 1674 | Py_PRINT_RAW); |
| 1675 | if (err) |
| 1676 | return NULL; |
| 1677 | } |
Eric Smith | 7c47894 | 2008-03-18 23:45:49 +0000 | [diff] [blame] | 1678 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1679 | if (end == NULL) |
| 1680 | err = PyFile_WriteObject(newline, file, Py_PRINT_RAW); |
| 1681 | else |
| 1682 | err = PyFile_WriteObject(end, file, Py_PRINT_RAW); |
| 1683 | if (err) |
| 1684 | return NULL; |
Eric Smith | 7c47894 | 2008-03-18 23:45:49 +0000 | [diff] [blame] | 1685 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1686 | Py_RETURN_NONE; |
Eric Smith | 7c47894 | 2008-03-18 23:45:49 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | PyDoc_STRVAR(print_doc, |
| 1690 | "print(value, ..., sep=' ', end='\\n', file=sys.stdout)\n\ |
| 1691 | \n\ |
| 1692 | Prints the values to a stream, or to sys.stdout by default.\n\ |
| 1693 | Optional keyword arguments:\n\ |
| 1694 | file: a file-like object (stream); defaults to the current sys.stdout.\n\ |
| 1695 | sep: string inserted between values, default a space.\n\ |
| 1696 | end: string appended after the last value, default a newline."); |
| 1697 | |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1698 | |
| 1699 | /* Return number of items in range (lo, hi, step), when arguments are |
| 1700 | * PyInt or PyLong objects. step > 0 required. Return a value < 0 if |
| 1701 | * & only if the true value is too large to fit in a signed long. |
| 1702 | * Arguments MUST return 1 with either PyInt_Check() or |
| 1703 | * PyLong_Check(). Return -1 when there is an error. |
| 1704 | */ |
| 1705 | static long |
| 1706 | get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step) |
| 1707 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1708 | /* ------------------------------------------------------------- |
| 1709 | Algorithm is equal to that of get_len_of_range(), but it operates |
| 1710 | on PyObjects (which are assumed to be PyLong or PyInt objects). |
| 1711 | ---------------------------------------------------------------*/ |
| 1712 | long n; |
| 1713 | PyObject *diff = NULL; |
| 1714 | PyObject *one = NULL; |
| 1715 | PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL; |
| 1716 | /* holds sub-expression evaluations */ |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1717 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1718 | /* if (lo >= hi), return length of 0. */ |
| 1719 | if (PyObject_Compare(lo, hi) >= 0) |
| 1720 | return 0; |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1721 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1722 | if ((one = PyLong_FromLong(1L)) == NULL) |
| 1723 | goto Fail; |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1724 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1725 | if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL) |
| 1726 | goto Fail; |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1727 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1728 | if ((diff = PyNumber_Subtract(tmp1, one)) == NULL) |
| 1729 | goto Fail; |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1730 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1731 | if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL) |
| 1732 | goto Fail; |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1733 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1734 | if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL) |
| 1735 | goto Fail; |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1736 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1737 | n = PyLong_AsLong(tmp3); |
| 1738 | if (PyErr_Occurred()) { /* Check for Overflow */ |
| 1739 | PyErr_Clear(); |
| 1740 | goto Fail; |
| 1741 | } |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1742 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1743 | Py_DECREF(tmp3); |
| 1744 | Py_DECREF(tmp2); |
| 1745 | Py_DECREF(diff); |
| 1746 | Py_DECREF(tmp1); |
| 1747 | Py_DECREF(one); |
| 1748 | return n; |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1749 | |
| 1750 | Fail: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1751 | Py_XDECREF(tmp3); |
| 1752 | Py_XDECREF(tmp2); |
| 1753 | Py_XDECREF(diff); |
| 1754 | Py_XDECREF(tmp1); |
| 1755 | Py_XDECREF(one); |
| 1756 | return -1; |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
Mark Dickinson | a8d2668 | 2010-05-04 16:18:25 +0000 | [diff] [blame] | 1759 | /* Helper function for handle_range_longs. If arg is int or long |
| 1760 | object, returns it with incremented reference count. If arg is |
| 1761 | float, raises type error. As a last resort, creates a new int by |
| 1762 | calling arg type's nb_int method if it is defined. Returns NULL |
| 1763 | and sets exception on error. |
| 1764 | |
| 1765 | Returns a new reference to an int object. */ |
| 1766 | static PyObject * |
| 1767 | get_range_long_argument(PyObject *arg, const char *name) |
| 1768 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1769 | PyObject *v; |
| 1770 | PyNumberMethods *nb; |
| 1771 | if (PyInt_Check(arg) || PyLong_Check(arg)) { |
| 1772 | Py_INCREF(arg); |
| 1773 | return arg; |
| 1774 | } |
| 1775 | if (PyFloat_Check(arg) || |
| 1776 | (nb = Py_TYPE(arg)->tp_as_number) == NULL || |
| 1777 | nb->nb_int == NULL) { |
| 1778 | PyErr_Format(PyExc_TypeError, |
| 1779 | "range() integer %s argument expected, got %s.", |
| 1780 | name, arg->ob_type->tp_name); |
| 1781 | return NULL; |
| 1782 | } |
| 1783 | v = nb->nb_int(arg); |
| 1784 | if (v == NULL) |
| 1785 | return NULL; |
| 1786 | if (PyInt_Check(v) || PyLong_Check(v)) |
| 1787 | return v; |
| 1788 | Py_DECREF(v); |
| 1789 | PyErr_SetString(PyExc_TypeError, |
| 1790 | "__int__ should return int object"); |
| 1791 | return NULL; |
Mark Dickinson | a8d2668 | 2010-05-04 16:18:25 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1794 | /* An extension of builtin_range() that handles the case when PyLong |
| 1795 | * arguments are given. */ |
| 1796 | static PyObject * |
Mark Dickinson | ef9b4ab | 2010-05-04 16:19:06 +0000 | [diff] [blame] | 1797 | handle_range_longs(PyObject *self, PyObject *args) |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1798 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1799 | PyObject *ilow = NULL; |
| 1800 | PyObject *ihigh = NULL; |
| 1801 | PyObject *istep = NULL; |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1802 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1803 | PyObject *low = NULL; |
| 1804 | PyObject *high = NULL; |
| 1805 | PyObject *step = NULL; |
Mark Dickinson | a8d2668 | 2010-05-04 16:18:25 +0000 | [diff] [blame] | 1806 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1807 | PyObject *curnum = NULL; |
| 1808 | PyObject *v = NULL; |
| 1809 | long bign; |
| 1810 | Py_ssize_t i, n; |
| 1811 | int cmp_result; |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1812 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1813 | PyObject *zero = PyLong_FromLong(0); |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1814 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1815 | if (zero == NULL) |
| 1816 | return NULL; |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1817 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1818 | if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) { |
| 1819 | Py_DECREF(zero); |
| 1820 | return NULL; |
| 1821 | } |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1822 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1823 | /* Figure out which way we were called, supply defaults, and be |
| 1824 | * sure to incref everything so that the decrefs at the end |
| 1825 | * are correct. NB: ilow, ihigh and istep are borrowed references. |
| 1826 | */ |
| 1827 | assert(ilow != NULL); |
| 1828 | if (ihigh == NULL) { |
| 1829 | /* only 1 arg -- it's the upper limit */ |
| 1830 | ihigh = ilow; |
| 1831 | ilow = NULL; |
| 1832 | } |
Mark Dickinson | a8d2668 | 2010-05-04 16:18:25 +0000 | [diff] [blame] | 1833 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1834 | /* convert ihigh if necessary */ |
| 1835 | assert(ihigh != NULL); |
| 1836 | high = get_range_long_argument(ihigh, "end"); |
| 1837 | if (high == NULL) |
| 1838 | goto Fail; |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1839 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1840 | /* ihigh correct now; do ilow */ |
| 1841 | if (ilow == NULL) { |
| 1842 | Py_INCREF(zero); |
| 1843 | low = zero; |
| 1844 | } |
| 1845 | else { |
| 1846 | low = get_range_long_argument(ilow, "start"); |
| 1847 | if (low == NULL) |
| 1848 | goto Fail; |
| 1849 | } |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1850 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1851 | /* ilow and ihigh correct now; do istep */ |
| 1852 | if (istep == NULL) |
| 1853 | step = PyLong_FromLong(1); |
| 1854 | else |
| 1855 | step = get_range_long_argument(istep, "step"); |
| 1856 | if (step == NULL) |
| 1857 | goto Fail; |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1858 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1859 | if (PyObject_Cmp(step, zero, &cmp_result) == -1) |
| 1860 | goto Fail; |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1861 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1862 | if (cmp_result == 0) { |
| 1863 | PyErr_SetString(PyExc_ValueError, |
| 1864 | "range() step argument must not be zero"); |
| 1865 | goto Fail; |
| 1866 | } |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1867 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1868 | if (cmp_result > 0) |
| 1869 | bign = get_len_of_range_longs(low, high, step); |
| 1870 | else { |
| 1871 | PyObject *neg_step = PyNumber_Negative(step); |
| 1872 | if (neg_step == NULL) |
| 1873 | goto Fail; |
| 1874 | bign = get_len_of_range_longs(high, low, neg_step); |
| 1875 | Py_DECREF(neg_step); |
| 1876 | } |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1877 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1878 | n = (Py_ssize_t)bign; |
| 1879 | if (bign < 0 || (long)n != bign) { |
| 1880 | PyErr_SetString(PyExc_OverflowError, |
| 1881 | "range() result has too many items"); |
| 1882 | goto Fail; |
| 1883 | } |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1884 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1885 | v = PyList_New(n); |
| 1886 | if (v == NULL) |
| 1887 | goto Fail; |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1888 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1889 | curnum = low; |
| 1890 | Py_INCREF(curnum); |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1891 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1892 | for (i = 0; i < n; i++) { |
| 1893 | PyObject *w = PyNumber_Long(curnum); |
| 1894 | PyObject *tmp_num; |
| 1895 | if (w == NULL) |
| 1896 | goto Fail; |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1897 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1898 | PyList_SET_ITEM(v, i, w); |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1899 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1900 | tmp_num = PyNumber_Add(curnum, step); |
| 1901 | if (tmp_num == NULL) |
| 1902 | goto Fail; |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1903 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1904 | Py_DECREF(curnum); |
| 1905 | curnum = tmp_num; |
| 1906 | } |
| 1907 | Py_DECREF(low); |
| 1908 | Py_DECREF(high); |
| 1909 | Py_DECREF(step); |
| 1910 | Py_DECREF(zero); |
| 1911 | Py_DECREF(curnum); |
| 1912 | return v; |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1913 | |
| 1914 | Fail: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1915 | Py_XDECREF(low); |
| 1916 | Py_XDECREF(high); |
| 1917 | Py_XDECREF(step); |
| 1918 | Py_DECREF(zero); |
| 1919 | Py_XDECREF(curnum); |
| 1920 | Py_XDECREF(v); |
| 1921 | return NULL; |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
Guido van Rossum | 124eff0 | 1999-02-23 16:11:01 +0000 | [diff] [blame] | 1924 | /* Return number of items in range/xrange (lo, hi, step). step > 0 |
| 1925 | * required. Return a value < 0 if & only if the true value is too |
| 1926 | * large to fit in a signed long. |
| 1927 | */ |
| 1928 | static long |
Thomas Wouters | e28c296 | 2000-07-23 22:21:32 +0000 | [diff] [blame] | 1929 | get_len_of_range(long lo, long hi, long step) |
Guido van Rossum | 124eff0 | 1999-02-23 16:11:01 +0000 | [diff] [blame] | 1930 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1931 | /* ------------------------------------------------------------- |
| 1932 | If lo >= hi, the range is empty. |
| 1933 | Else if n values are in the range, the last one is |
| 1934 | lo + (n-1)*step, which must be <= hi-1. Rearranging, |
| 1935 | n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives |
| 1936 | the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so |
| 1937 | the RHS is non-negative and so truncation is the same as the |
| 1938 | floor. Letting M be the largest positive long, the worst case |
| 1939 | for the RHS numerator is hi=M, lo=-M-1, and then |
| 1940 | hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough |
| 1941 | precision to compute the RHS exactly. |
| 1942 | ---------------------------------------------------------------*/ |
| 1943 | long n = 0; |
| 1944 | if (lo < hi) { |
| 1945 | unsigned long uhi = (unsigned long)hi; |
| 1946 | unsigned long ulo = (unsigned long)lo; |
| 1947 | unsigned long diff = uhi - ulo - 1; |
| 1948 | n = (long)(diff / (unsigned long)step + 1); |
| 1949 | } |
| 1950 | return n; |
Guido van Rossum | 124eff0 | 1999-02-23 16:11:01 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1953 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1954 | builtin_range(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1955 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1956 | long ilow = 0, ihigh = 0, istep = 1; |
| 1957 | long bign; |
| 1958 | Py_ssize_t i, n; |
Guido van Rossum | 124eff0 | 1999-02-23 16:11:01 +0000 | [diff] [blame] | 1959 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1960 | PyObject *v; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1961 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1962 | if (PyTuple_Size(args) <= 1) { |
| 1963 | if (!PyArg_ParseTuple(args, |
| 1964 | "l;range() requires 1-3 int arguments", |
| 1965 | &ihigh)) { |
| 1966 | PyErr_Clear(); |
| 1967 | return handle_range_longs(self, args); |
| 1968 | } |
| 1969 | } |
| 1970 | else { |
| 1971 | if (!PyArg_ParseTuple(args, |
| 1972 | "ll|l;range() requires 1-3 int arguments", |
| 1973 | &ilow, &ihigh, &istep)) { |
| 1974 | PyErr_Clear(); |
| 1975 | return handle_range_longs(self, args); |
| 1976 | } |
| 1977 | } |
| 1978 | if (istep == 0) { |
| 1979 | PyErr_SetString(PyExc_ValueError, |
| 1980 | "range() step argument must not be zero"); |
| 1981 | return NULL; |
| 1982 | } |
| 1983 | if (istep > 0) |
| 1984 | bign = get_len_of_range(ilow, ihigh, istep); |
| 1985 | else |
| 1986 | bign = get_len_of_range(ihigh, ilow, -istep); |
| 1987 | n = (Py_ssize_t)bign; |
| 1988 | if (bign < 0 || (long)n != bign) { |
| 1989 | PyErr_SetString(PyExc_OverflowError, |
| 1990 | "range() result has too many items"); |
| 1991 | return NULL; |
| 1992 | } |
| 1993 | v = PyList_New(n); |
| 1994 | if (v == NULL) |
| 1995 | return NULL; |
| 1996 | for (i = 0; i < n; i++) { |
| 1997 | PyObject *w = PyInt_FromLong(ilow); |
| 1998 | if (w == NULL) { |
| 1999 | Py_DECREF(v); |
| 2000 | return NULL; |
| 2001 | } |
| 2002 | PyList_SET_ITEM(v, i, w); |
| 2003 | ilow += istep; |
| 2004 | } |
| 2005 | return v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2008 | PyDoc_STRVAR(range_doc, |
Chris Jerdonek | ad4b000 | 2012-10-07 20:37:54 -0700 | [diff] [blame] | 2009 | "range(stop) -> list of integers\n\ |
| 2010 | range(start, stop[, step]) -> list of integers\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2011 | \n\ |
| 2012 | Return a list containing an arithmetic progression of integers.\n\ |
| 2013 | range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\ |
| 2014 | When step is given, it specifies the increment (or decrement).\n\ |
| 2015 | For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2016 | These are exactly the valid indices for a list of 4 elements."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2017 | |
| 2018 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2019 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2020 | builtin_raw_input(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2021 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2022 | PyObject *v = NULL; |
| 2023 | PyObject *fin = PySys_GetObject("stdin"); |
| 2024 | PyObject *fout = PySys_GetObject("stdout"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2025 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2026 | if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v)) |
| 2027 | return NULL; |
Martin v. Löwis | 31d2df5 | 2002-08-14 15:46:02 +0000 | [diff] [blame] | 2028 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2029 | if (fin == NULL) { |
| 2030 | PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin"); |
| 2031 | return NULL; |
| 2032 | } |
| 2033 | if (fout == NULL) { |
| 2034 | PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout"); |
| 2035 | return NULL; |
| 2036 | } |
| 2037 | if (PyFile_SoftSpace(fout, 0)) { |
| 2038 | if (PyFile_WriteString(" ", fout) != 0) |
| 2039 | return NULL; |
| 2040 | } |
| 2041 | if (PyFile_AsFile(fin) && PyFile_AsFile(fout) |
| 2042 | && isatty(fileno(PyFile_AsFile(fin))) |
| 2043 | && isatty(fileno(PyFile_AsFile(fout)))) { |
| 2044 | PyObject *po; |
| 2045 | char *prompt; |
| 2046 | char *s; |
| 2047 | PyObject *result; |
| 2048 | if (v != NULL) { |
| 2049 | po = PyObject_Str(v); |
| 2050 | if (po == NULL) |
| 2051 | return NULL; |
| 2052 | prompt = PyString_AsString(po); |
| 2053 | if (prompt == NULL) |
| 2054 | return NULL; |
| 2055 | } |
| 2056 | else { |
| 2057 | po = NULL; |
| 2058 | prompt = ""; |
| 2059 | } |
| 2060 | s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout), |
| 2061 | prompt); |
| 2062 | Py_XDECREF(po); |
| 2063 | if (s == NULL) { |
| 2064 | if (!PyErr_Occurred()) |
| 2065 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
| 2066 | return NULL; |
| 2067 | } |
| 2068 | if (*s == '\0') { |
| 2069 | PyErr_SetNone(PyExc_EOFError); |
| 2070 | result = NULL; |
| 2071 | } |
| 2072 | else { /* strip trailing '\n' */ |
| 2073 | size_t len = strlen(s); |
| 2074 | if (len > PY_SSIZE_T_MAX) { |
| 2075 | PyErr_SetString(PyExc_OverflowError, |
| 2076 | "[raw_]input: input too long"); |
| 2077 | result = NULL; |
| 2078 | } |
| 2079 | else { |
| 2080 | result = PyString_FromStringAndSize(s, len-1); |
| 2081 | } |
| 2082 | } |
| 2083 | PyMem_FREE(s); |
| 2084 | return result; |
| 2085 | } |
| 2086 | if (v != NULL) { |
| 2087 | if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0) |
| 2088 | return NULL; |
| 2089 | } |
| 2090 | return PyFile_GetLine(fin, -1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2093 | PyDoc_STRVAR(raw_input_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2094 | "raw_input([prompt]) -> string\n\ |
| 2095 | \n\ |
| 2096 | Read a string from standard input. The trailing newline is stripped.\n\ |
| 2097 | If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\ |
| 2098 | On Unix, GNU readline is used if enabled. The prompt string, if given,\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2099 | is printed without a trailing newline before reading."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2100 | |
| 2101 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2102 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2103 | builtin_reduce(PyObject *self, PyObject *args) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2104 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2105 | static PyObject *functools_reduce = NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2106 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2107 | if (PyErr_WarnPy3k("reduce() not supported in 3.x; " |
| 2108 | "use functools.reduce()", 1) < 0) |
| 2109 | return NULL; |
Neal Norwitz | df25efe | 2007-05-23 06:58:36 +0000 | [diff] [blame] | 2110 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2111 | if (functools_reduce == NULL) { |
| 2112 | PyObject *functools = PyImport_ImportModule("functools"); |
| 2113 | if (functools == NULL) |
| 2114 | return NULL; |
| 2115 | functools_reduce = PyObject_GetAttrString(functools, "reduce"); |
| 2116 | Py_DECREF(functools); |
| 2117 | if (functools_reduce == NULL) |
| 2118 | return NULL; |
| 2119 | } |
| 2120 | return PyObject_Call(functools_reduce, args, NULL); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2121 | } |
| 2122 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2123 | PyDoc_STRVAR(reduce_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2124 | "reduce(function, sequence[, initial]) -> value\n\ |
| 2125 | \n\ |
| 2126 | Apply a function of two arguments cumulatively to the items of a sequence,\n\ |
| 2127 | from left to right, so as to reduce the sequence to a single value.\n\ |
| 2128 | For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\ |
| 2129 | ((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\ |
| 2130 | of the sequence in the calculation, and serves as a default when the\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2131 | sequence is empty."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2132 | |
| 2133 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2134 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2135 | builtin_reload(PyObject *self, PyObject *v) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2136 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2137 | if (PyErr_WarnPy3k("In 3.x, reload() is renamed to imp.reload()", |
| 2138 | 1) < 0) |
| 2139 | return NULL; |
Neal Norwitz | df25efe | 2007-05-23 06:58:36 +0000 | [diff] [blame] | 2140 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2141 | return PyImport_ReloadModule(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2142 | } |
| 2143 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2144 | PyDoc_STRVAR(reload_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2145 | "reload(module) -> module\n\ |
| 2146 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2147 | Reload the module. The module must have been successfully imported before."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2148 | |
| 2149 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2150 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2151 | builtin_repr(PyObject *self, PyObject *v) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2152 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2153 | return PyObject_Repr(v); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2156 | PyDoc_STRVAR(repr_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2157 | "repr(object) -> string\n\ |
| 2158 | \n\ |
| 2159 | Return the canonical string representation of the object.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2160 | For most object types, eval(repr(object)) == object."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2161 | |
| 2162 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2163 | static PyObject * |
Georg Brandl | ccadf84 | 2006-03-31 18:54:53 +0000 | [diff] [blame] | 2164 | builtin_round(PyObject *self, PyObject *args, PyObject *kwds) |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 2165 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2166 | double x; |
| 2167 | PyObject *o_ndigits = NULL; |
| 2168 | Py_ssize_t ndigits; |
| 2169 | static char *kwlist[] = {"number", "ndigits", 0}; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2170 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2171 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|O:round", |
| 2172 | kwlist, &x, &o_ndigits)) |
| 2173 | return NULL; |
Mark Dickinson | bd15a06 | 2009-11-18 19:33:35 +0000 | [diff] [blame] | 2174 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2175 | if (o_ndigits == NULL) { |
| 2176 | /* second argument defaults to 0 */ |
| 2177 | ndigits = 0; |
| 2178 | } |
| 2179 | else { |
| 2180 | /* interpret 2nd argument as a Py_ssize_t; clip on overflow */ |
| 2181 | ndigits = PyNumber_AsSsize_t(o_ndigits, NULL); |
| 2182 | if (ndigits == -1 && PyErr_Occurred()) |
| 2183 | return NULL; |
| 2184 | } |
Mark Dickinson | bd15a06 | 2009-11-18 19:33:35 +0000 | [diff] [blame] | 2185 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2186 | /* nans, infinities and zeros round to themselves */ |
| 2187 | if (!Py_IS_FINITE(x) || x == 0.0) |
| 2188 | return PyFloat_FromDouble(x); |
Mark Dickinson | bce7837 | 2009-11-24 10:54:58 +0000 | [diff] [blame] | 2189 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2190 | /* Deal with extreme values for ndigits. For ndigits > NDIGITS_MAX, x |
| 2191 | always rounds to itself. For ndigits < NDIGITS_MIN, x always |
| 2192 | rounds to +-0.0. Here 0.30103 is an upper bound for log10(2). */ |
Mark Dickinson | bd15a06 | 2009-11-18 19:33:35 +0000 | [diff] [blame] | 2193 | #define NDIGITS_MAX ((int)((DBL_MANT_DIG-DBL_MIN_EXP) * 0.30103)) |
| 2194 | #define NDIGITS_MIN (-(int)((DBL_MAX_EXP + 1) * 0.30103)) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2195 | if (ndigits > NDIGITS_MAX) |
| 2196 | /* return x */ |
| 2197 | return PyFloat_FromDouble(x); |
| 2198 | else if (ndigits < NDIGITS_MIN) |
| 2199 | /* return 0.0, but with sign of x */ |
| 2200 | return PyFloat_FromDouble(0.0*x); |
| 2201 | else |
| 2202 | /* finite x, and ndigits is not unreasonably large */ |
| 2203 | /* _Py_double_round is defined in floatobject.c */ |
| 2204 | return _Py_double_round(x, (int)ndigits); |
Mark Dickinson | bd15a06 | 2009-11-18 19:33:35 +0000 | [diff] [blame] | 2205 | #undef NDIGITS_MAX |
| 2206 | #undef NDIGITS_MIN |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 2207 | } |
| 2208 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2209 | PyDoc_STRVAR(round_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2210 | "round(number[, ndigits]) -> floating point number\n\ |
| 2211 | \n\ |
| 2212 | Round a number to a given precision in decimal digits (default 0 digits).\n\ |
Jeffrey Yasskin | 9871d8f | 2008-01-05 08:47:13 +0000 | [diff] [blame] | 2213 | This always returns a floating point number. Precision may be negative."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2214 | |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 2215 | static PyObject * |
| 2216 | builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds) |
| 2217 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2218 | PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs; |
| 2219 | PyObject *callable; |
| 2220 | static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0}; |
| 2221 | int reverse; |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 2222 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2223 | /* args 1-4 should match listsort in Objects/listobject.c */ |
| 2224 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted", |
| 2225 | kwlist, &seq, &compare, &keyfunc, &reverse)) |
| 2226 | return NULL; |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 2227 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2228 | newlist = PySequence_List(seq); |
| 2229 | if (newlist == NULL) |
| 2230 | return NULL; |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 2231 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2232 | callable = PyObject_GetAttrString(newlist, "sort"); |
| 2233 | if (callable == NULL) { |
| 2234 | Py_DECREF(newlist); |
| 2235 | return NULL; |
| 2236 | } |
Georg Brandl | 99d7e4e | 2005-08-31 22:21:15 +0000 | [diff] [blame] | 2237 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2238 | newargs = PyTuple_GetSlice(args, 1, 4); |
| 2239 | if (newargs == NULL) { |
| 2240 | Py_DECREF(newlist); |
| 2241 | Py_DECREF(callable); |
| 2242 | return NULL; |
| 2243 | } |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 2244 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2245 | v = PyObject_Call(callable, newargs, kwds); |
| 2246 | Py_DECREF(newargs); |
| 2247 | Py_DECREF(callable); |
| 2248 | if (v == NULL) { |
| 2249 | Py_DECREF(newlist); |
| 2250 | return NULL; |
| 2251 | } |
| 2252 | Py_DECREF(v); |
| 2253 | return newlist; |
Raymond Hettinger | 64958a1 | 2003-12-17 20:43:33 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
| 2256 | PyDoc_STRVAR(sorted_doc, |
| 2257 | "sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list"); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2258 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2259 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2260 | builtin_vars(PyObject *self, PyObject *args) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 2261 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2262 | PyObject *v = NULL; |
| 2263 | PyObject *d; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 2264 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2265 | if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v)) |
| 2266 | return NULL; |
| 2267 | if (v == NULL) { |
| 2268 | d = PyEval_GetLocals(); |
| 2269 | if (d == NULL) { |
| 2270 | if (!PyErr_Occurred()) |
| 2271 | PyErr_SetString(PyExc_SystemError, |
| 2272 | "vars(): no locals!?"); |
| 2273 | } |
| 2274 | else |
| 2275 | Py_INCREF(d); |
| 2276 | } |
| 2277 | else { |
| 2278 | d = PyObject_GetAttrString(v, "__dict__"); |
| 2279 | if (d == NULL) { |
| 2280 | PyErr_SetString(PyExc_TypeError, |
| 2281 | "vars() argument must have __dict__ attribute"); |
| 2282 | return NULL; |
| 2283 | } |
| 2284 | } |
| 2285 | return d; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 2286 | } |
| 2287 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2288 | PyDoc_STRVAR(vars_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2289 | "vars([object]) -> dictionary\n\ |
| 2290 | \n\ |
| 2291 | Without arguments, equivalent to locals().\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2292 | With an argument, equivalent to object.__dict__."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2293 | |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 2294 | |
| 2295 | static PyObject* |
| 2296 | builtin_sum(PyObject *self, PyObject *args) |
| 2297 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2298 | PyObject *seq; |
| 2299 | PyObject *result = NULL; |
| 2300 | PyObject *temp, *item, *iter; |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 2301 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2302 | if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result)) |
| 2303 | return NULL; |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 2304 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2305 | iter = PyObject_GetIter(seq); |
| 2306 | if (iter == NULL) |
| 2307 | return NULL; |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 2308 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2309 | if (result == NULL) { |
| 2310 | result = PyInt_FromLong(0); |
| 2311 | if (result == NULL) { |
| 2312 | Py_DECREF(iter); |
| 2313 | return NULL; |
| 2314 | } |
| 2315 | } else { |
| 2316 | /* reject string values for 'start' parameter */ |
| 2317 | if (PyObject_TypeCheck(result, &PyBaseString_Type)) { |
| 2318 | PyErr_SetString(PyExc_TypeError, |
| 2319 | "sum() can't sum strings [use ''.join(seq) instead]"); |
| 2320 | Py_DECREF(iter); |
| 2321 | return NULL; |
| 2322 | } |
| 2323 | Py_INCREF(result); |
| 2324 | } |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 2325 | |
Raymond Hettinger | 3f8caa3 | 2007-10-24 01:28:33 +0000 | [diff] [blame] | 2326 | #ifndef SLOW_SUM |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2327 | /* Fast addition by keeping temporary sums in C instead of new Python objects. |
| 2328 | Assumes all inputs are the same type. If the assumption fails, default |
| 2329 | to the more general routine. |
| 2330 | */ |
| 2331 | if (PyInt_CheckExact(result)) { |
| 2332 | long i_result = PyInt_AS_LONG(result); |
| 2333 | Py_DECREF(result); |
| 2334 | result = NULL; |
| 2335 | while(result == NULL) { |
| 2336 | item = PyIter_Next(iter); |
| 2337 | if (item == NULL) { |
| 2338 | Py_DECREF(iter); |
| 2339 | if (PyErr_Occurred()) |
| 2340 | return NULL; |
| 2341 | return PyInt_FromLong(i_result); |
| 2342 | } |
| 2343 | if (PyInt_CheckExact(item)) { |
| 2344 | long b = PyInt_AS_LONG(item); |
| 2345 | long x = i_result + b; |
| 2346 | if ((x^i_result) >= 0 || (x^b) >= 0) { |
| 2347 | i_result = x; |
| 2348 | Py_DECREF(item); |
| 2349 | continue; |
| 2350 | } |
| 2351 | } |
| 2352 | /* Either overflowed or is not an int. Restore real objects and process normally */ |
| 2353 | result = PyInt_FromLong(i_result); |
| 2354 | temp = PyNumber_Add(result, item); |
| 2355 | Py_DECREF(result); |
| 2356 | Py_DECREF(item); |
| 2357 | result = temp; |
| 2358 | if (result == NULL) { |
| 2359 | Py_DECREF(iter); |
| 2360 | return NULL; |
| 2361 | } |
| 2362 | } |
| 2363 | } |
Raymond Hettinger | 3f8caa3 | 2007-10-24 01:28:33 +0000 | [diff] [blame] | 2364 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2365 | if (PyFloat_CheckExact(result)) { |
| 2366 | double f_result = PyFloat_AS_DOUBLE(result); |
| 2367 | Py_DECREF(result); |
| 2368 | result = NULL; |
| 2369 | while(result == NULL) { |
| 2370 | item = PyIter_Next(iter); |
| 2371 | if (item == NULL) { |
| 2372 | Py_DECREF(iter); |
| 2373 | if (PyErr_Occurred()) |
| 2374 | return NULL; |
| 2375 | return PyFloat_FromDouble(f_result); |
| 2376 | } |
| 2377 | if (PyFloat_CheckExact(item)) { |
| 2378 | PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0) |
| 2379 | f_result += PyFloat_AS_DOUBLE(item); |
| 2380 | PyFPE_END_PROTECT(f_result) |
| 2381 | Py_DECREF(item); |
| 2382 | continue; |
| 2383 | } |
| 2384 | if (PyInt_CheckExact(item)) { |
| 2385 | PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0) |
| 2386 | f_result += (double)PyInt_AS_LONG(item); |
| 2387 | PyFPE_END_PROTECT(f_result) |
| 2388 | Py_DECREF(item); |
| 2389 | continue; |
| 2390 | } |
| 2391 | result = PyFloat_FromDouble(f_result); |
| 2392 | temp = PyNumber_Add(result, item); |
| 2393 | Py_DECREF(result); |
| 2394 | Py_DECREF(item); |
| 2395 | result = temp; |
| 2396 | if (result == NULL) { |
| 2397 | Py_DECREF(iter); |
| 2398 | return NULL; |
| 2399 | } |
| 2400 | } |
| 2401 | } |
Raymond Hettinger | 3f8caa3 | 2007-10-24 01:28:33 +0000 | [diff] [blame] | 2402 | #endif |
| 2403 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2404 | for(;;) { |
| 2405 | item = PyIter_Next(iter); |
| 2406 | if (item == NULL) { |
| 2407 | /* error, or end-of-sequence */ |
| 2408 | if (PyErr_Occurred()) { |
| 2409 | Py_DECREF(result); |
| 2410 | result = NULL; |
| 2411 | } |
| 2412 | break; |
| 2413 | } |
| 2414 | /* It's tempting to use PyNumber_InPlaceAdd instead of |
| 2415 | PyNumber_Add here, to avoid quadratic running time |
| 2416 | when doing 'sum(list_of_lists, [])'. However, this |
| 2417 | would produce a change in behaviour: a snippet like |
Mark Dickinson | 0e0e215 | 2009-10-26 14:18:44 +0000 | [diff] [blame] | 2418 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2419 | empty = [] |
| 2420 | sum([[x] for x in range(10)], empty) |
Mark Dickinson | 0e0e215 | 2009-10-26 14:18:44 +0000 | [diff] [blame] | 2421 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2422 | would change the value of empty. */ |
| 2423 | temp = PyNumber_Add(result, item); |
| 2424 | Py_DECREF(result); |
| 2425 | Py_DECREF(item); |
| 2426 | result = temp; |
| 2427 | if (result == NULL) |
| 2428 | break; |
| 2429 | } |
| 2430 | Py_DECREF(iter); |
| 2431 | return result; |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 2432 | } |
| 2433 | |
| 2434 | PyDoc_STRVAR(sum_doc, |
Georg Brandl | 8134d06 | 2006-10-12 12:33:07 +0000 | [diff] [blame] | 2435 | "sum(sequence[, start]) -> value\n\ |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 2436 | \n\ |
R David Murray | f7c8584 | 2013-07-10 16:23:15 -0400 | [diff] [blame] | 2437 | Return the sum of a sequence of numbers (NOT strings) plus the value\n\ |
Georg Brandl | 8134d06 | 2006-10-12 12:33:07 +0000 | [diff] [blame] | 2438 | of parameter 'start' (which defaults to 0). When the sequence is\n\ |
R David Murray | f7c8584 | 2013-07-10 16:23:15 -0400 | [diff] [blame] | 2439 | empty, return start."); |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 2440 | |
| 2441 | |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 2442 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2443 | builtin_isinstance(PyObject *self, PyObject *args) |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 2444 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2445 | PyObject *inst; |
| 2446 | PyObject *cls; |
| 2447 | int retval; |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 2448 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2449 | if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls)) |
| 2450 | return NULL; |
Guido van Rossum | f5dd914 | 1997-12-02 19:11:45 +0000 | [diff] [blame] | 2451 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2452 | retval = PyObject_IsInstance(inst, cls); |
| 2453 | if (retval < 0) |
| 2454 | return NULL; |
| 2455 | return PyBool_FromLong(retval); |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 2456 | } |
| 2457 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2458 | PyDoc_STRVAR(isinstance_doc, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 2459 | "isinstance(object, class-or-type-or-tuple) -> bool\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2460 | \n\ |
| 2461 | 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] | 2462 | With a type as second argument, return whether that is the object's type.\n\ |
| 2463 | 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] | 2464 | isinstance(x, A) or isinstance(x, B) or ... (etc.)."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2465 | |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 2466 | |
| 2467 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2468 | builtin_issubclass(PyObject *self, PyObject *args) |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 2469 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2470 | PyObject *derived; |
| 2471 | PyObject *cls; |
| 2472 | int retval; |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 2473 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2474 | if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls)) |
| 2475 | return NULL; |
Guido van Rossum | 668213d | 1999-06-16 17:28:37 +0000 | [diff] [blame] | 2476 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2477 | retval = PyObject_IsSubclass(derived, cls); |
| 2478 | if (retval < 0) |
| 2479 | return NULL; |
| 2480 | return PyBool_FromLong(retval); |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 2481 | } |
| 2482 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2483 | PyDoc_STRVAR(issubclass_doc, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 2484 | "issubclass(C, B) -> bool\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2485 | \n\ |
Walter Dörwald | d9a6ad3 | 2002-12-12 16:41:44 +0000 | [diff] [blame] | 2486 | Return whether class C is a subclass (i.e., a derived class) of class B.\n\ |
| 2487 | When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\ |
| 2488 | 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] | 2489 | |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 2490 | |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2491 | static PyObject* |
| 2492 | builtin_zip(PyObject *self, PyObject *args) |
| 2493 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2494 | PyObject *ret; |
| 2495 | const Py_ssize_t itemsize = PySequence_Length(args); |
| 2496 | Py_ssize_t i; |
| 2497 | PyObject *itlist; /* tuple of iterators */ |
| 2498 | Py_ssize_t len; /* guess at result length */ |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2499 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2500 | if (itemsize == 0) |
| 2501 | return PyList_New(0); |
Raymond Hettinger | eaef615 | 2003-08-02 07:42:57 +0000 | [diff] [blame] | 2502 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2503 | /* args must be a tuple */ |
| 2504 | assert(PyTuple_Check(args)); |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2505 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2506 | /* Guess at result length: the shortest of the input lengths. |
| 2507 | If some argument refuses to say, we refuse to guess too, lest |
| 2508 | an argument like xrange(sys.maxint) lead us astray.*/ |
| 2509 | len = -1; /* unknown */ |
| 2510 | for (i = 0; i < itemsize; ++i) { |
| 2511 | PyObject *item = PyTuple_GET_ITEM(args, i); |
| 2512 | Py_ssize_t thislen = _PyObject_LengthHint(item, -2); |
| 2513 | if (thislen < 0) { |
| 2514 | if (thislen == -1) |
| 2515 | return NULL; |
| 2516 | len = -1; |
| 2517 | break; |
| 2518 | } |
| 2519 | else if (len < 0 || thislen < len) |
| 2520 | len = thislen; |
| 2521 | } |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 2522 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2523 | /* allocate result list */ |
| 2524 | if (len < 0) |
| 2525 | len = 10; /* arbitrary */ |
| 2526 | if ((ret = PyList_New(len)) == NULL) |
| 2527 | return NULL; |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2528 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2529 | /* obtain iterators */ |
| 2530 | itlist = PyTuple_New(itemsize); |
| 2531 | if (itlist == NULL) |
| 2532 | goto Fail_ret; |
| 2533 | for (i = 0; i < itemsize; ++i) { |
| 2534 | PyObject *item = PyTuple_GET_ITEM(args, i); |
| 2535 | PyObject *it = PyObject_GetIter(item); |
| 2536 | if (it == NULL) { |
| 2537 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 2538 | PyErr_Format(PyExc_TypeError, |
| 2539 | "zip argument #%zd must support iteration", |
| 2540 | i+1); |
| 2541 | goto Fail_ret_itlist; |
| 2542 | } |
| 2543 | PyTuple_SET_ITEM(itlist, i, it); |
| 2544 | } |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2545 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2546 | /* build result into ret list */ |
| 2547 | for (i = 0; ; ++i) { |
| 2548 | int j; |
| 2549 | PyObject *next = PyTuple_New(itemsize); |
| 2550 | if (!next) |
| 2551 | goto Fail_ret_itlist; |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 2552 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2553 | for (j = 0; j < itemsize; j++) { |
| 2554 | PyObject *it = PyTuple_GET_ITEM(itlist, j); |
| 2555 | PyObject *item = PyIter_Next(it); |
| 2556 | if (!item) { |
| 2557 | if (PyErr_Occurred()) { |
| 2558 | Py_DECREF(ret); |
| 2559 | ret = NULL; |
| 2560 | } |
| 2561 | Py_DECREF(next); |
| 2562 | Py_DECREF(itlist); |
| 2563 | goto Done; |
| 2564 | } |
| 2565 | PyTuple_SET_ITEM(next, j, item); |
| 2566 | } |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 2567 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2568 | if (i < len) |
| 2569 | PyList_SET_ITEM(ret, i, next); |
| 2570 | else { |
| 2571 | int status = PyList_Append(ret, next); |
| 2572 | Py_DECREF(next); |
| 2573 | ++len; |
| 2574 | if (status < 0) |
| 2575 | goto Fail_ret_itlist; |
| 2576 | } |
| 2577 | } |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 2578 | |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 2579 | Done: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2580 | if (ret != NULL && i < len) { |
| 2581 | /* The list is too big. */ |
| 2582 | if (PyList_SetSlice(ret, i, len, NULL) < 0) |
| 2583 | return NULL; |
| 2584 | } |
| 2585 | return ret; |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 2586 | |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 2587 | Fail_ret_itlist: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2588 | Py_DECREF(itlist); |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 2589 | Fail_ret: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2590 | Py_DECREF(ret); |
| 2591 | return NULL; |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2592 | } |
| 2593 | |
| 2594 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2595 | PyDoc_STRVAR(zip_doc, |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2596 | "zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\ |
| 2597 | \n\ |
| 2598 | Return a list of tuples, where each tuple contains the i-th element\n\ |
| 2599 | from each of the argument sequences. The returned list is truncated\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2600 | in length to the length of the shortest argument sequence."); |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2601 | |
| 2602 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2603 | static PyMethodDef builtin_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2604 | {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc}, |
| 2605 | {"abs", builtin_abs, METH_O, abs_doc}, |
| 2606 | {"all", builtin_all, METH_O, all_doc}, |
| 2607 | {"any", builtin_any, METH_O, any_doc}, |
| 2608 | {"apply", builtin_apply, METH_VARARGS, apply_doc}, |
| 2609 | {"bin", builtin_bin, METH_O, bin_doc}, |
| 2610 | {"callable", builtin_callable, METH_O, callable_doc}, |
| 2611 | {"chr", builtin_chr, METH_VARARGS, chr_doc}, |
| 2612 | {"cmp", builtin_cmp, METH_VARARGS, cmp_doc}, |
| 2613 | {"coerce", builtin_coerce, METH_VARARGS, coerce_doc}, |
| 2614 | {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc}, |
| 2615 | {"delattr", builtin_delattr, METH_VARARGS, delattr_doc}, |
| 2616 | {"dir", builtin_dir, METH_VARARGS, dir_doc}, |
| 2617 | {"divmod", builtin_divmod, METH_VARARGS, divmod_doc}, |
| 2618 | {"eval", builtin_eval, METH_VARARGS, eval_doc}, |
| 2619 | {"execfile", builtin_execfile, METH_VARARGS, execfile_doc}, |
| 2620 | {"filter", builtin_filter, METH_VARARGS, filter_doc}, |
| 2621 | {"format", builtin_format, METH_VARARGS, format_doc}, |
| 2622 | {"getattr", builtin_getattr, METH_VARARGS, getattr_doc}, |
| 2623 | {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc}, |
| 2624 | {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc}, |
| 2625 | {"hash", builtin_hash, METH_O, hash_doc}, |
| 2626 | {"hex", builtin_hex, METH_O, hex_doc}, |
| 2627 | {"id", builtin_id, METH_O, id_doc}, |
| 2628 | {"input", builtin_input, METH_VARARGS, input_doc}, |
| 2629 | {"intern", builtin_intern, METH_VARARGS, intern_doc}, |
| 2630 | {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc}, |
| 2631 | {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc}, |
| 2632 | {"iter", builtin_iter, METH_VARARGS, iter_doc}, |
| 2633 | {"len", builtin_len, METH_O, len_doc}, |
| 2634 | {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc}, |
| 2635 | {"map", builtin_map, METH_VARARGS, map_doc}, |
| 2636 | {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc}, |
| 2637 | {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc}, |
| 2638 | {"next", builtin_next, METH_VARARGS, next_doc}, |
| 2639 | {"oct", builtin_oct, METH_O, oct_doc}, |
| 2640 | {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc}, |
| 2641 | {"ord", builtin_ord, METH_O, ord_doc}, |
| 2642 | {"pow", builtin_pow, METH_VARARGS, pow_doc}, |
| 2643 | {"print", (PyCFunction)builtin_print, METH_VARARGS | METH_KEYWORDS, print_doc}, |
| 2644 | {"range", builtin_range, METH_VARARGS, range_doc}, |
| 2645 | {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc}, |
| 2646 | {"reduce", builtin_reduce, METH_VARARGS, reduce_doc}, |
| 2647 | {"reload", builtin_reload, METH_O, reload_doc}, |
| 2648 | {"repr", builtin_repr, METH_O, repr_doc}, |
| 2649 | {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc}, |
| 2650 | {"setattr", builtin_setattr, METH_VARARGS, setattr_doc}, |
| 2651 | {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc}, |
| 2652 | {"sum", builtin_sum, METH_VARARGS, sum_doc}, |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2653 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2654 | {"unichr", builtin_unichr, METH_VARARGS, unichr_doc}, |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2655 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2656 | {"vars", builtin_vars, METH_VARARGS, vars_doc}, |
| 2657 | {"zip", builtin_zip, METH_VARARGS, zip_doc}, |
| 2658 | {NULL, NULL}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2659 | }; |
| 2660 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2661 | PyDoc_STRVAR(builtin_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2662 | "Built-in functions, exceptions, and other objects.\n\ |
| 2663 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2664 | Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2665 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2666 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2667 | _PyBuiltin_Init(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2668 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2669 | PyObject *mod, *dict, *debug; |
| 2670 | mod = Py_InitModule4("__builtin__", builtin_methods, |
| 2671 | builtin_doc, (PyObject *)NULL, |
| 2672 | PYTHON_API_VERSION); |
| 2673 | if (mod == NULL) |
| 2674 | return NULL; |
| 2675 | dict = PyModule_GetDict(mod); |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2676 | |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 2677 | #ifdef Py_TRACE_REFS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2678 | /* __builtin__ exposes a number of statically allocated objects |
| 2679 | * that, before this code was added in 2.3, never showed up in |
| 2680 | * the list of "all objects" maintained by Py_TRACE_REFS. As a |
| 2681 | * result, programs leaking references to None and False (etc) |
| 2682 | * couldn't be diagnosed by examining sys.getobjects(0). |
| 2683 | */ |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 2684 | #define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0) |
| 2685 | #else |
| 2686 | #define ADD_TO_ALL(OBJECT) (void)0 |
| 2687 | #endif |
| 2688 | |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2689 | #define SETBUILTIN(NAME, OBJECT) \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2690 | if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \ |
| 2691 | return NULL; \ |
| 2692 | ADD_TO_ALL(OBJECT) |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2693 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2694 | SETBUILTIN("None", Py_None); |
| 2695 | SETBUILTIN("Ellipsis", Py_Ellipsis); |
| 2696 | SETBUILTIN("NotImplemented", Py_NotImplemented); |
| 2697 | SETBUILTIN("False", Py_False); |
| 2698 | SETBUILTIN("True", Py_True); |
| 2699 | SETBUILTIN("basestring", &PyBaseString_Type); |
| 2700 | SETBUILTIN("bool", &PyBool_Type); |
| 2701 | SETBUILTIN("memoryview", &PyMemoryView_Type); |
| 2702 | SETBUILTIN("bytearray", &PyByteArray_Type); |
| 2703 | SETBUILTIN("bytes", &PyString_Type); |
| 2704 | SETBUILTIN("buffer", &PyBuffer_Type); |
| 2705 | SETBUILTIN("classmethod", &PyClassMethod_Type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2706 | #ifndef WITHOUT_COMPLEX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2707 | SETBUILTIN("complex", &PyComplex_Type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2708 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2709 | SETBUILTIN("dict", &PyDict_Type); |
| 2710 | SETBUILTIN("enumerate", &PyEnum_Type); |
| 2711 | SETBUILTIN("file", &PyFile_Type); |
| 2712 | SETBUILTIN("float", &PyFloat_Type); |
| 2713 | SETBUILTIN("frozenset", &PyFrozenSet_Type); |
| 2714 | SETBUILTIN("property", &PyProperty_Type); |
| 2715 | SETBUILTIN("int", &PyInt_Type); |
| 2716 | SETBUILTIN("list", &PyList_Type); |
| 2717 | SETBUILTIN("long", &PyLong_Type); |
| 2718 | SETBUILTIN("object", &PyBaseObject_Type); |
| 2719 | SETBUILTIN("reversed", &PyReversed_Type); |
| 2720 | SETBUILTIN("set", &PySet_Type); |
| 2721 | SETBUILTIN("slice", &PySlice_Type); |
| 2722 | SETBUILTIN("staticmethod", &PyStaticMethod_Type); |
| 2723 | SETBUILTIN("str", &PyString_Type); |
| 2724 | SETBUILTIN("super", &PySuper_Type); |
| 2725 | SETBUILTIN("tuple", &PyTuple_Type); |
| 2726 | SETBUILTIN("type", &PyType_Type); |
| 2727 | SETBUILTIN("xrange", &PyRange_Type); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2728 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2729 | SETBUILTIN("unicode", &PyUnicode_Type); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2730 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2731 | debug = PyBool_FromLong(Py_OptimizeFlag == 0); |
| 2732 | if (PyDict_SetItemString(dict, "__debug__", debug) < 0) { |
| 2733 | Py_XDECREF(debug); |
| 2734 | return NULL; |
| 2735 | } |
| 2736 | Py_XDECREF(debug); |
Barry Warsaw | 757af0e | 1997-08-29 22:13:51 +0000 | [diff] [blame] | 2737 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2738 | return mod; |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 2739 | #undef ADD_TO_ALL |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2740 | #undef SETBUILTIN |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 2743 | /* Helper for filter(): filter a tuple through a function */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2744 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2745 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2746 | filtertuple(PyObject *func, PyObject *tuple) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2747 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2748 | PyObject *result; |
| 2749 | Py_ssize_t i, j; |
| 2750 | Py_ssize_t len = PyTuple_Size(tuple); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2751 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2752 | if (len == 0) { |
| 2753 | if (PyTuple_CheckExact(tuple)) |
| 2754 | Py_INCREF(tuple); |
| 2755 | else |
| 2756 | tuple = PyTuple_New(0); |
| 2757 | return tuple; |
| 2758 | } |
Guido van Rossum | b7b4562 | 1995-08-04 04:07:45 +0000 | [diff] [blame] | 2759 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2760 | if ((result = PyTuple_New(len)) == NULL) |
| 2761 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2762 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2763 | for (i = j = 0; i < len; ++i) { |
| 2764 | PyObject *item, *good; |
| 2765 | int ok; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2766 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2767 | if (tuple->ob_type->tp_as_sequence && |
| 2768 | tuple->ob_type->tp_as_sequence->sq_item) { |
| 2769 | item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i); |
| 2770 | if (item == NULL) |
| 2771 | goto Fail_1; |
| 2772 | } else { |
| 2773 | PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple"); |
| 2774 | goto Fail_1; |
| 2775 | } |
| 2776 | if (func == Py_None) { |
| 2777 | Py_INCREF(item); |
| 2778 | good = item; |
| 2779 | } |
| 2780 | else { |
| 2781 | PyObject *arg = PyTuple_Pack(1, item); |
| 2782 | if (arg == NULL) { |
| 2783 | Py_DECREF(item); |
| 2784 | goto Fail_1; |
| 2785 | } |
| 2786 | good = PyEval_CallObject(func, arg); |
| 2787 | Py_DECREF(arg); |
| 2788 | if (good == NULL) { |
| 2789 | Py_DECREF(item); |
| 2790 | goto Fail_1; |
| 2791 | } |
| 2792 | } |
| 2793 | ok = PyObject_IsTrue(good); |
| 2794 | Py_DECREF(good); |
Antoine Pitrou | c5bef75 | 2012-08-15 23:16:51 +0200 | [diff] [blame] | 2795 | if (ok > 0) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2796 | if (PyTuple_SetItem(result, j++, item) < 0) |
| 2797 | goto Fail_1; |
| 2798 | } |
Antoine Pitrou | c5bef75 | 2012-08-15 23:16:51 +0200 | [diff] [blame] | 2799 | else { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2800 | Py_DECREF(item); |
Antoine Pitrou | c5bef75 | 2012-08-15 23:16:51 +0200 | [diff] [blame] | 2801 | if (ok < 0) |
| 2802 | goto Fail_1; |
| 2803 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2804 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2805 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2806 | if (_PyTuple_Resize(&result, j) < 0) |
| 2807 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2808 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2809 | return result; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2810 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2811 | Fail_1: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2812 | Py_DECREF(result); |
| 2813 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2814 | } |
| 2815 | |
| 2816 | |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 2817 | /* Helper for filter(): filter a string through a function */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2818 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2819 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2820 | filterstring(PyObject *func, PyObject *strobj) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2821 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2822 | PyObject *result; |
| 2823 | Py_ssize_t i, j; |
| 2824 | Py_ssize_t len = PyString_Size(strobj); |
| 2825 | Py_ssize_t outlen = len; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2826 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2827 | if (func == Py_None) { |
| 2828 | /* If it's a real string we can return the original, |
| 2829 | * as no character is ever false and __getitem__ |
| 2830 | * does return this character. If it's a subclass |
| 2831 | * we must go through the __getitem__ loop */ |
| 2832 | if (PyString_CheckExact(strobj)) { |
| 2833 | Py_INCREF(strobj); |
| 2834 | return strobj; |
| 2835 | } |
| 2836 | } |
| 2837 | if ((result = PyString_FromStringAndSize(NULL, len)) == NULL) |
| 2838 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2839 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2840 | for (i = j = 0; i < len; ++i) { |
| 2841 | PyObject *item; |
| 2842 | int ok; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2843 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2844 | item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i); |
| 2845 | if (item == NULL) |
| 2846 | goto Fail_1; |
| 2847 | if (func==Py_None) { |
| 2848 | ok = 1; |
| 2849 | } else { |
| 2850 | PyObject *arg, *good; |
| 2851 | arg = PyTuple_Pack(1, item); |
| 2852 | if (arg == NULL) { |
| 2853 | Py_DECREF(item); |
| 2854 | goto Fail_1; |
| 2855 | } |
| 2856 | good = PyEval_CallObject(func, arg); |
| 2857 | Py_DECREF(arg); |
| 2858 | if (good == NULL) { |
| 2859 | Py_DECREF(item); |
| 2860 | goto Fail_1; |
| 2861 | } |
| 2862 | ok = PyObject_IsTrue(good); |
| 2863 | Py_DECREF(good); |
| 2864 | } |
Antoine Pitrou | c5bef75 | 2012-08-15 23:16:51 +0200 | [diff] [blame] | 2865 | if (ok > 0) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2866 | Py_ssize_t reslen; |
| 2867 | if (!PyString_Check(item)) { |
| 2868 | PyErr_SetString(PyExc_TypeError, "can't filter str to str:" |
| 2869 | " __getitem__ returned different type"); |
| 2870 | Py_DECREF(item); |
| 2871 | goto Fail_1; |
| 2872 | } |
| 2873 | reslen = PyString_GET_SIZE(item); |
| 2874 | if (reslen == 1) { |
| 2875 | PyString_AS_STRING(result)[j++] = |
| 2876 | PyString_AS_STRING(item)[0]; |
| 2877 | } else { |
| 2878 | /* do we need more space? */ |
| 2879 | Py_ssize_t need = j; |
Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 2880 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2881 | /* calculate space requirements while checking for overflow */ |
| 2882 | if (need > PY_SSIZE_T_MAX - reslen) { |
| 2883 | Py_DECREF(item); |
| 2884 | goto Fail_1; |
| 2885 | } |
Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 2886 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2887 | need += reslen; |
Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 2888 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2889 | if (need > PY_SSIZE_T_MAX - len) { |
| 2890 | Py_DECREF(item); |
| 2891 | goto Fail_1; |
| 2892 | } |
Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 2893 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2894 | need += len; |
Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 2895 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2896 | if (need <= i) { |
| 2897 | Py_DECREF(item); |
| 2898 | goto Fail_1; |
| 2899 | } |
Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 2900 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2901 | need = need - i - 1; |
Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 2902 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2903 | assert(need >= 0); |
| 2904 | assert(outlen >= 0); |
Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 2905 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2906 | if (need > outlen) { |
| 2907 | /* overallocate, to avoid reallocations */ |
| 2908 | if (outlen > PY_SSIZE_T_MAX / 2) { |
| 2909 | Py_DECREF(item); |
| 2910 | return NULL; |
| 2911 | } |
Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 2912 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2913 | if (need<2*outlen) { |
| 2914 | need = 2*outlen; |
| 2915 | } |
| 2916 | if (_PyString_Resize(&result, need)) { |
| 2917 | Py_DECREF(item); |
| 2918 | return NULL; |
| 2919 | } |
| 2920 | outlen = need; |
| 2921 | } |
| 2922 | memcpy( |
| 2923 | PyString_AS_STRING(result) + j, |
| 2924 | PyString_AS_STRING(item), |
| 2925 | reslen |
| 2926 | ); |
| 2927 | j += reslen; |
| 2928 | } |
| 2929 | } |
| 2930 | Py_DECREF(item); |
Antoine Pitrou | c5bef75 | 2012-08-15 23:16:51 +0200 | [diff] [blame] | 2931 | if (ok < 0) |
| 2932 | goto Fail_1; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2933 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2934 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2935 | if (j < outlen) |
| 2936 | _PyString_Resize(&result, j); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2937 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2938 | return result; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2939 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2940 | Fail_1: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2941 | Py_DECREF(result); |
| 2942 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2943 | } |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 2944 | |
| 2945 | #ifdef Py_USING_UNICODE |
| 2946 | /* Helper for filter(): filter a Unicode object through a function */ |
| 2947 | |
| 2948 | static PyObject * |
| 2949 | filterunicode(PyObject *func, PyObject *strobj) |
| 2950 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2951 | PyObject *result; |
| 2952 | register Py_ssize_t i, j; |
| 2953 | Py_ssize_t len = PyUnicode_GetSize(strobj); |
| 2954 | Py_ssize_t outlen = len; |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 2955 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2956 | if (func == Py_None) { |
| 2957 | /* If it's a real string we can return the original, |
| 2958 | * as no character is ever false and __getitem__ |
| 2959 | * does return this character. If it's a subclass |
| 2960 | * we must go through the __getitem__ loop */ |
| 2961 | if (PyUnicode_CheckExact(strobj)) { |
| 2962 | Py_INCREF(strobj); |
| 2963 | return strobj; |
| 2964 | } |
| 2965 | } |
| 2966 | if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL) |
| 2967 | return NULL; |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 2968 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2969 | for (i = j = 0; i < len; ++i) { |
| 2970 | PyObject *item, *arg, *good; |
| 2971 | int ok; |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 2972 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2973 | item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i); |
| 2974 | if (item == NULL) |
| 2975 | goto Fail_1; |
| 2976 | if (func == Py_None) { |
| 2977 | ok = 1; |
| 2978 | } else { |
| 2979 | arg = PyTuple_Pack(1, item); |
| 2980 | if (arg == NULL) { |
| 2981 | Py_DECREF(item); |
| 2982 | goto Fail_1; |
| 2983 | } |
| 2984 | good = PyEval_CallObject(func, arg); |
| 2985 | Py_DECREF(arg); |
| 2986 | if (good == NULL) { |
| 2987 | Py_DECREF(item); |
| 2988 | goto Fail_1; |
| 2989 | } |
| 2990 | ok = PyObject_IsTrue(good); |
| 2991 | Py_DECREF(good); |
| 2992 | } |
Antoine Pitrou | c5bef75 | 2012-08-15 23:16:51 +0200 | [diff] [blame] | 2993 | if (ok > 0) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2994 | Py_ssize_t reslen; |
| 2995 | if (!PyUnicode_Check(item)) { |
| 2996 | PyErr_SetString(PyExc_TypeError, |
| 2997 | "can't filter unicode to unicode:" |
| 2998 | " __getitem__ returned different type"); |
| 2999 | Py_DECREF(item); |
| 3000 | goto Fail_1; |
| 3001 | } |
| 3002 | reslen = PyUnicode_GET_SIZE(item); |
| 3003 | if (reslen == 1) |
| 3004 | PyUnicode_AS_UNICODE(result)[j++] = |
| 3005 | PyUnicode_AS_UNICODE(item)[0]; |
| 3006 | else { |
| 3007 | /* do we need more space? */ |
| 3008 | Py_ssize_t need = j + reslen + len - i - 1; |
Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 3009 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3010 | /* check that didnt overflow */ |
| 3011 | if ((j > PY_SSIZE_T_MAX - reslen) || |
| 3012 | ((j + reslen) > PY_SSIZE_T_MAX - len) || |
| 3013 | ((j + reslen + len) < i) || |
| 3014 | ((j + reslen + len - i) <= 0)) { |
| 3015 | Py_DECREF(item); |
| 3016 | return NULL; |
| 3017 | } |
Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 3018 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3019 | assert(need >= 0); |
| 3020 | assert(outlen >= 0); |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 3021 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3022 | if (need > outlen) { |
| 3023 | /* overallocate, |
| 3024 | to avoid reallocations */ |
| 3025 | if (need < 2 * outlen) { |
| 3026 | if (outlen > PY_SSIZE_T_MAX / 2) { |
| 3027 | Py_DECREF(item); |
| 3028 | return NULL; |
| 3029 | } else { |
| 3030 | need = 2 * outlen; |
| 3031 | } |
| 3032 | } |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 3033 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3034 | if (PyUnicode_Resize( |
| 3035 | &result, need) < 0) { |
| 3036 | Py_DECREF(item); |
| 3037 | goto Fail_1; |
| 3038 | } |
| 3039 | outlen = need; |
| 3040 | } |
| 3041 | memcpy(PyUnicode_AS_UNICODE(result) + j, |
| 3042 | PyUnicode_AS_UNICODE(item), |
| 3043 | reslen*sizeof(Py_UNICODE)); |
| 3044 | j += reslen; |
| 3045 | } |
| 3046 | } |
| 3047 | Py_DECREF(item); |
Antoine Pitrou | c5bef75 | 2012-08-15 23:16:51 +0200 | [diff] [blame] | 3048 | if (ok < 0) |
| 3049 | goto Fail_1; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3050 | } |
| 3051 | |
| 3052 | if (j < outlen) |
| 3053 | PyUnicode_Resize(&result, j); |
| 3054 | |
| 3055 | return result; |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 3056 | |
| 3057 | Fail_1: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3058 | Py_DECREF(result); |
| 3059 | return NULL; |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 3060 | } |
| 3061 | #endif |