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