Andrew M. Kuchling | 9bcc68c | 2000-12-20 15:07:34 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2 | /* Built-in functions */ |
| 3 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 5 | |
| 6 | #include "node.h" |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 7 | #include "compile.h" |
| 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> |
| 11 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 12 | #ifdef RISCOS |
| 13 | #include "unixstuff.h" |
| 14 | #endif |
| 15 | |
Mark Hammond | 26cffde4 | 2001-05-14 12:17:34 +0000 | [diff] [blame] | 16 | /* The default encoding used by the platform file system APIs |
| 17 | Can remain NULL for all platforms that don't have such a concept |
| 18 | */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 19 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Mark Hammond | 26cffde4 | 2001-05-14 12:17:34 +0000 | [diff] [blame] | 20 | const char *Py_FileSystemDefaultEncoding = "mbcs"; |
Just van Rossum | b9b8e9c | 2003-02-10 09:22:01 +0000 | [diff] [blame] | 21 | #elif defined(__APPLE__) |
| 22 | const char *Py_FileSystemDefaultEncoding = "utf-8"; |
Mark Hammond | 26cffde4 | 2001-05-14 12:17:34 +0000 | [diff] [blame] | 23 | #else |
| 24 | const char *Py_FileSystemDefaultEncoding = NULL; /* use default */ |
| 25 | #endif |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 27 | /* Forward */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 28 | static PyObject *filterstring(PyObject *, PyObject *); |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 29 | #ifdef Py_USING_UNICODE |
| 30 | static PyObject *filterunicode(PyObject *, PyObject *); |
| 31 | #endif |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 32 | static PyObject *filtertuple (PyObject *, PyObject *); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 34 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 35 | builtin___import__(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 36 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 37 | char *name; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 38 | PyObject *globals = NULL; |
| 39 | PyObject *locals = NULL; |
| 40 | PyObject *fromlist = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 42 | if (!PyArg_ParseTuple(args, "s|OOO:__import__", |
Guido van Rossum | 24c1374 | 1995-02-14 09:42:43 +0000 | [diff] [blame] | 43 | &name, &globals, &locals, &fromlist)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 44 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 45 | return PyImport_ImportModuleEx(name, globals, locals, fromlist); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 48 | PyDoc_STRVAR(import_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 49 | "__import__(name, globals, locals, fromlist) -> module\n\ |
| 50 | \n\ |
| 51 | Import a module. The globals are only used to determine the context;\n\ |
| 52 | they are not modified. The locals are currently unused. The fromlist\n\ |
| 53 | should be a list of names to emulate ``from name import ...'', or an\n\ |
| 54 | empty list to emulate ``import name''.\n\ |
| 55 | When importing a module from a package, note that __import__('A.B', ...)\n\ |
| 56 | returns package A when fromlist is empty, but its submodule B when\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 57 | fromlist is not empty."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 58 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 59 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 60 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 61 | builtin_abs(PyObject *self, PyObject *v) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 62 | { |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 63 | return PyNumber_Absolute(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 66 | PyDoc_STRVAR(abs_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 67 | "abs(number) -> number\n\ |
| 68 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 69 | Return the absolute value of the argument."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 70 | |
| 71 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 72 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 73 | builtin_apply(PyObject *self, PyObject *args) |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 74 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 75 | PyObject *func, *alist = NULL, *kwdict = NULL; |
Barry Warsaw | 968f8cb | 1998-10-01 15:33:12 +0000 | [diff] [blame] | 76 | PyObject *t = NULL, *retval = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 77 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 78 | if (!PyArg_UnpackTuple(args, "apply", 1, 3, &func, &alist, &kwdict)) |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 79 | return NULL; |
Barry Warsaw | 968f8cb | 1998-10-01 15:33:12 +0000 | [diff] [blame] | 80 | if (alist != NULL) { |
| 81 | if (!PyTuple_Check(alist)) { |
| 82 | if (!PySequence_Check(alist)) { |
Jeremy Hylton | c862cf4 | 2001-01-19 03:25:05 +0000 | [diff] [blame] | 83 | PyErr_Format(PyExc_TypeError, |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 84 | "apply() arg 2 expected sequence, found %s", |
Jeremy Hylton | c862cf4 | 2001-01-19 03:25:05 +0000 | [diff] [blame] | 85 | alist->ob_type->tp_name); |
Barry Warsaw | 968f8cb | 1998-10-01 15:33:12 +0000 | [diff] [blame] | 86 | return NULL; |
| 87 | } |
| 88 | t = PySequence_Tuple(alist); |
| 89 | if (t == NULL) |
| 90 | return NULL; |
| 91 | alist = t; |
| 92 | } |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 93 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 94 | if (kwdict != NULL && !PyDict_Check(kwdict)) { |
Jeremy Hylton | c862cf4 | 2001-01-19 03:25:05 +0000 | [diff] [blame] | 95 | PyErr_Format(PyExc_TypeError, |
| 96 | "apply() arg 3 expected dictionary, found %s", |
| 97 | kwdict->ob_type->tp_name); |
Barry Warsaw | 968f8cb | 1998-10-01 15:33:12 +0000 | [diff] [blame] | 98 | goto finally; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 99 | } |
Barry Warsaw | 968f8cb | 1998-10-01 15:33:12 +0000 | [diff] [blame] | 100 | retval = PyEval_CallObjectWithKeywords(func, alist, kwdict); |
| 101 | finally: |
| 102 | Py_XDECREF(t); |
| 103 | return retval; |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 106 | PyDoc_STRVAR(apply_doc, |
Fred Drake | f1fbc62 | 2001-01-12 17:05:05 +0000 | [diff] [blame] | 107 | "apply(object[, args[, kwargs]]) -> value\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 108 | \n\ |
Fred Drake | 7b91212 | 1999-12-23 14:16:55 +0000 | [diff] [blame] | 109 | Call a callable object with positional arguments taken from the tuple args,\n\ |
| 110 | and keyword arguments taken from the optional dictionary kwargs.\n\ |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 111 | Note that classes are callable, as are instances with a __call__() method.\n\ |
| 112 | \n\ |
| 113 | Deprecated since release 2.3. Instead, use the extended call syntax:\n\ |
| 114 | function(*args, **keywords)."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 115 | |
| 116 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 117 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 118 | builtin_callable(PyObject *self, PyObject *v) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 119 | { |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 120 | return PyBool_FromLong((long)PyCallable_Check(v)); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 123 | PyDoc_STRVAR(callable_doc, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 124 | "callable(object) -> bool\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 125 | \n\ |
| 126 | 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] | 127 | 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] | 128 | |
| 129 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 130 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 131 | builtin_filter(PyObject *self, PyObject *args) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 132 | { |
Guido van Rossum | c7903a1 | 2002-08-16 07:04:56 +0000 | [diff] [blame] | 133 | PyObject *func, *seq, *result, *it, *arg; |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 134 | int len; /* guess for result list size */ |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 135 | register int j; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 136 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 137 | if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq)) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 138 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 139 | |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 140 | /* Strings and tuples return a result of the same type. */ |
| 141 | if (PyString_Check(seq)) |
| 142 | return filterstring(func, seq); |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 143 | #ifdef Py_USING_UNICODE |
| 144 | if (PyUnicode_Check(seq)) |
| 145 | return filterunicode(func, seq); |
| 146 | #endif |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 147 | if (PyTuple_Check(seq)) |
| 148 | return filtertuple(func, seq); |
| 149 | |
| 150 | /* Get iterator. */ |
| 151 | it = PyObject_GetIter(seq); |
| 152 | if (it == NULL) |
| 153 | return NULL; |
| 154 | |
| 155 | /* Guess a result list size. */ |
| 156 | len = -1; /* unknown */ |
| 157 | if (PySequence_Check(seq) && |
| 158 | seq->ob_type->tp_as_sequence->sq_length) { |
| 159 | len = PySequence_Size(seq); |
| 160 | if (len < 0) |
| 161 | PyErr_Clear(); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 162 | } |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 163 | if (len < 0) |
| 164 | len = 8; /* arbitrary */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 165 | |
Guido van Rossum | c7903a1 | 2002-08-16 07:04:56 +0000 | [diff] [blame] | 166 | /* Pre-allocate argument list tuple. */ |
| 167 | arg = PyTuple_New(1); |
| 168 | if (arg == NULL) |
| 169 | goto Fail_arg; |
| 170 | |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 171 | /* Get a result list. */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 172 | if (PyList_Check(seq) && seq->ob_refcnt == 1) { |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 173 | /* Eww - can modify the list in-place. */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 174 | Py_INCREF(seq); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 175 | result = seq; |
| 176 | } |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 177 | else { |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 178 | result = PyList_New(len); |
| 179 | if (result == NULL) |
| 180 | goto Fail_it; |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 181 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 182 | |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 183 | /* Build the result list. */ |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 184 | j = 0; |
| 185 | for (;;) { |
Guido van Rossum | c7903a1 | 2002-08-16 07:04:56 +0000 | [diff] [blame] | 186 | PyObject *item; |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 187 | int ok; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 188 | |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 189 | item = PyIter_Next(it); |
| 190 | if (item == NULL) { |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 191 | if (PyErr_Occurred()) |
| 192 | goto Fail_result_it; |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 193 | break; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 194 | } |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 195 | |
Neil Schemenauer | 6897355 | 2003-08-14 20:37:34 +0000 | [diff] [blame] | 196 | if (func == (PyObject *)&PyBool_Type || func == Py_None) { |
Guido van Rossum | c7903a1 | 2002-08-16 07:04:56 +0000 | [diff] [blame] | 197 | ok = PyObject_IsTrue(item); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 198 | } |
| 199 | else { |
Guido van Rossum | c7903a1 | 2002-08-16 07:04:56 +0000 | [diff] [blame] | 200 | PyObject *good; |
| 201 | PyTuple_SET_ITEM(arg, 0, item); |
| 202 | good = PyObject_Call(func, arg, NULL); |
| 203 | PyTuple_SET_ITEM(arg, 0, NULL); |
Guido van Rossum | 58b6873 | 1995-01-10 17:40:55 +0000 | [diff] [blame] | 204 | if (good == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 205 | Py_DECREF(item); |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 206 | goto Fail_result_it; |
Guido van Rossum | 58b6873 | 1995-01-10 17:40:55 +0000 | [diff] [blame] | 207 | } |
Guido van Rossum | c7903a1 | 2002-08-16 07:04:56 +0000 | [diff] [blame] | 208 | ok = PyObject_IsTrue(good); |
| 209 | Py_DECREF(good); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 210 | } |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 211 | if (ok) { |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 212 | if (j < len) |
| 213 | PyList_SET_ITEM(result, j, item); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 214 | else { |
Barry Warsaw | fa77e09 | 1999-01-28 18:49:12 +0000 | [diff] [blame] | 215 | int status = PyList_Append(result, item); |
Barry Warsaw | fa77e09 | 1999-01-28 18:49:12 +0000 | [diff] [blame] | 216 | Py_DECREF(item); |
| 217 | if (status < 0) |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 218 | goto Fail_result_it; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 219 | } |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 220 | ++j; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 221 | } |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 222 | else |
| 223 | Py_DECREF(item); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 226 | |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 227 | /* Cut back result list if len is too big. */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 228 | if (j < len && PyList_SetSlice(result, j, len, NULL) < 0) |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 229 | goto Fail_result_it; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 230 | |
Tim Peters | 3c6b148 | 2001-05-21 08:07:05 +0000 | [diff] [blame] | 231 | Py_DECREF(it); |
Guido van Rossum | c7903a1 | 2002-08-16 07:04:56 +0000 | [diff] [blame] | 232 | Py_DECREF(arg); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 233 | return result; |
| 234 | |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 235 | Fail_result_it: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 236 | Py_DECREF(result); |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 237 | Fail_it: |
| 238 | Py_DECREF(it); |
Guido van Rossum | c7903a1 | 2002-08-16 07:04:56 +0000 | [diff] [blame] | 239 | Fail_arg: |
| 240 | Py_DECREF(arg); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 241 | return NULL; |
| 242 | } |
| 243 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 244 | PyDoc_STRVAR(filter_doc, |
Tim Peters | d50e544 | 2002-03-09 00:06:26 +0000 | [diff] [blame] | 245 | "filter(function or None, sequence) -> list, tuple, or string\n" |
| 246 | "\n" |
| 247 | "Return those items of sequence for which function(item) is true. If\n" |
| 248 | "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] | 249 | "or string, return the same type, else return a list."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 250 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 251 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 252 | builtin_chr(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 253 | { |
| 254 | long x; |
| 255 | char s[1]; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 256 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 257 | if (!PyArg_ParseTuple(args, "l:chr", &x)) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 258 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 259 | if (x < 0 || x >= 256) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 260 | PyErr_SetString(PyExc_ValueError, |
| 261 | "chr() arg not in range(256)"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 262 | return NULL; |
| 263 | } |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 264 | s[0] = (char)x; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 265 | return PyString_FromStringAndSize(s, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 268 | PyDoc_STRVAR(chr_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 269 | "chr(i) -> character\n\ |
| 270 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 271 | 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] | 272 | |
| 273 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 274 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 275 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 276 | builtin_unichr(PyObject *self, PyObject *args) |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 277 | { |
| 278 | long x; |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 279 | |
| 280 | if (!PyArg_ParseTuple(args, "l:unichr", &x)) |
| 281 | return NULL; |
Fredrik Lundh | 0dcf67e | 2001-06-26 20:01:56 +0000 | [diff] [blame] | 282 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 283 | return PyUnicode_FromOrdinal(x); |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 286 | PyDoc_STRVAR(unichr_doc, |
Fred Drake | 078b24f | 2000-04-13 02:42:50 +0000 | [diff] [blame] | 287 | "unichr(i) -> Unicode character\n\ |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 288 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 289 | 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] | 290 | #endif |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 291 | |
| 292 | |
| 293 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 294 | builtin_cmp(PyObject *self, PyObject *args) |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 295 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 296 | PyObject *a, *b; |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 297 | int c; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 298 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 299 | if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b)) |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 300 | return NULL; |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 301 | if (PyObject_Cmp(a, b, &c) < 0) |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 302 | return NULL; |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 303 | return PyInt_FromLong((long)c); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 306 | PyDoc_STRVAR(cmp_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 307 | "cmp(x, y) -> integer\n\ |
| 308 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 309 | 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] | 310 | |
| 311 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 312 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 313 | builtin_coerce(PyObject *self, PyObject *args) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 314 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 315 | PyObject *v, *w; |
| 316 | PyObject *res; |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 317 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 318 | if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w)) |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 319 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 320 | if (PyNumber_Coerce(&v, &w) < 0) |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 321 | return NULL; |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 322 | res = PyTuple_Pack(2, v, w); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 323 | Py_DECREF(v); |
| 324 | Py_DECREF(w); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 325 | return res; |
| 326 | } |
| 327 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 328 | PyDoc_STRVAR(coerce_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 329 | "coerce(x, y) -> None or (x1, y1)\n\ |
| 330 | \n\ |
| 331 | When x and y can be coerced to values of the same type, return a tuple\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 332 | containing the coerced values. When they can't be coerced, return None."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 333 | |
| 334 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 335 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 336 | builtin_compile(PyObject *self, PyObject *args) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 337 | { |
| 338 | char *str; |
| 339 | char *filename; |
| 340 | char *startstr; |
| 341 | int start; |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 342 | int dont_inherit = 0; |
| 343 | int supplied_flags = 0; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 344 | PyCompilerFlags cf; |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 345 | PyObject *result, *cmd, *tmp = NULL; |
Just van Rossum | b9b8e9c | 2003-02-10 09:22:01 +0000 | [diff] [blame] | 346 | int length; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 347 | |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 348 | if (!PyArg_ParseTuple(args, "Oss|ii:compile", &cmd, &filename, |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 349 | &startstr, &supplied_flags, &dont_inherit)) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 350 | return NULL; |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 351 | |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 352 | cf.cf_flags = supplied_flags; |
| 353 | |
| 354 | #ifdef Py_USING_UNICODE |
| 355 | if (PyUnicode_Check(cmd)) { |
| 356 | tmp = PyUnicode_AsUTF8String(cmd); |
| 357 | if (tmp == NULL) |
| 358 | return NULL; |
| 359 | cmd = tmp; |
| 360 | cf.cf_flags |= PyCF_SOURCE_IS_UTF8; |
| 361 | } |
| 362 | #endif |
Just van Rossum | b9b8e9c | 2003-02-10 09:22:01 +0000 | [diff] [blame] | 363 | if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length)) |
| 364 | return NULL; |
Tim Peters | 2c646c9 | 2003-02-10 14:48:29 +0000 | [diff] [blame] | 365 | if ((size_t)length != strlen(str)) { |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 366 | PyErr_SetString(PyExc_TypeError, |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 367 | "compile() expected string without null bytes"); |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 368 | return NULL; |
| 369 | } |
| 370 | |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 371 | if (strcmp(startstr, "exec") == 0) |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 372 | start = Py_file_input; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 373 | else if (strcmp(startstr, "eval") == 0) |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 374 | start = Py_eval_input; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 375 | else if (strcmp(startstr, "single") == 0) |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 376 | start = Py_single_input; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 377 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 378 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 379 | "compile() arg 3 must be 'exec' or 'eval' or 'single'"); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 380 | return NULL; |
| 381 | } |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 382 | |
Guido van Rossum | 4b499dd3 | 2003-02-13 22:07:59 +0000 | [diff] [blame] | 383 | if (supplied_flags & |
| 384 | ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT)) |
| 385 | { |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 386 | PyErr_SetString(PyExc_ValueError, |
| 387 | "compile(): unrecognised flags"); |
| 388 | return NULL; |
| 389 | } |
| 390 | /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */ |
| 391 | |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 392 | if (!dont_inherit) { |
| 393 | PyEval_MergeCompilerFlags(&cf); |
| 394 | } |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 395 | result = Py_CompileStringFlags(str, filename, start, &cf); |
| 396 | Py_XDECREF(tmp); |
| 397 | return result; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 400 | PyDoc_STRVAR(compile_doc, |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 401 | "compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 402 | \n\ |
| 403 | Compile the source string (a Python module, statement or expression)\n\ |
| 404 | into a code object that can be executed by the exec statement or eval().\n\ |
| 405 | The filename will be used for run-time error messages.\n\ |
| 406 | 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] | 407 | single (interactive) statement, or 'eval' to compile an expression.\n\ |
| 408 | The flags argument, if present, controls which future statements influence\n\ |
| 409 | the compilation of the code.\n\ |
| 410 | The dont_inherit argument, if non-zero, stops the compilation inheriting\n\ |
| 411 | the effects of any future statements in effect in the code calling\n\ |
| 412 | 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] | 413 | in addition to any features explicitly specified."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 414 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 415 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 416 | builtin_dir(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 417 | { |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 418 | PyObject *arg = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 419 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 420 | if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 421 | return NULL; |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 422 | return PyObject_Dir(arg); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 425 | PyDoc_STRVAR(dir_doc, |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 426 | "dir([object]) -> list of strings\n" |
| 427 | "\n" |
| 428 | "Return an alphabetized list of names comprising (some of) the attributes\n" |
| 429 | "of the given object, and of attributes reachable from it:\n" |
| 430 | "\n" |
| 431 | "No argument: the names in the current scope.\n" |
| 432 | "Module object: the module attributes.\n" |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 433 | "Type or class object: its attributes, and recursively the attributes of\n" |
| 434 | " its bases.\n" |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 435 | "Otherwise: its attributes, its class's attributes, and recursively the\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 436 | " attributes of its class's base classes."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 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_divmod(PyObject *self, PyObject *args) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 440 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 441 | PyObject *v, *w; |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 442 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 443 | if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w)) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 444 | return NULL; |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 445 | return PyNumber_Divmod(v, w); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 448 | PyDoc_STRVAR(divmod_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 449 | "divmod(x, y) -> (div, mod)\n\ |
| 450 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 451 | 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] | 452 | |
| 453 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 454 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 455 | builtin_eval(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 456 | { |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 457 | PyObject *cmd, *result, *tmp = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 458 | PyObject *globals = Py_None, *locals = Py_None; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 459 | char *str; |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 460 | PyCompilerFlags cf; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 461 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 462 | if (!PyArg_ParseTuple(args, "O|O!O!:eval", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 463 | &cmd, |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 464 | &PyDict_Type, &globals, |
| 465 | &PyDict_Type, &locals)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 466 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 467 | if (globals == Py_None) { |
| 468 | globals = PyEval_GetGlobals(); |
| 469 | if (locals == Py_None) |
| 470 | locals = PyEval_GetLocals(); |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 471 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 472 | else if (locals == Py_None) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 473 | locals = globals; |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 474 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 475 | if (PyDict_GetItemString(globals, "__builtins__") == NULL) { |
| 476 | if (PyDict_SetItemString(globals, "__builtins__", |
| 477 | PyEval_GetBuiltins()) != 0) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 478 | return NULL; |
| 479 | } |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 480 | |
Jeremy Hylton | 15c1c4f | 2001-07-30 21:50:55 +0000 | [diff] [blame] | 481 | if (PyCode_Check(cmd)) { |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 482 | if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) { |
Jeremy Hylton | 15c1c4f | 2001-07-30 21:50:55 +0000 | [diff] [blame] | 483 | PyErr_SetString(PyExc_TypeError, |
| 484 | "code object passed to eval() may not contain free variables"); |
| 485 | return NULL; |
| 486 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 487 | return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals); |
Jeremy Hylton | 15c1c4f | 2001-07-30 21:50:55 +0000 | [diff] [blame] | 488 | } |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 489 | |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 490 | if (!PyString_Check(cmd) && |
| 491 | !PyUnicode_Check(cmd)) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 492 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 493 | "eval() arg 1 must be a string or code object"); |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 494 | return NULL; |
| 495 | } |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 496 | cf.cf_flags = 0; |
| 497 | |
| 498 | #ifdef Py_USING_UNICODE |
| 499 | if (PyUnicode_Check(cmd)) { |
| 500 | tmp = PyUnicode_AsUTF8String(cmd); |
| 501 | if (tmp == NULL) |
| 502 | return NULL; |
| 503 | cmd = tmp; |
| 504 | cf.cf_flags |= PyCF_SOURCE_IS_UTF8; |
| 505 | } |
| 506 | #endif |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 507 | if (PyString_AsStringAndSize(cmd, &str, NULL)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 508 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 509 | while (*str == ' ' || *str == '\t') |
| 510 | str++; |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 511 | |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 512 | (void)PyEval_MergeCompilerFlags(&cf); |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 513 | result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf); |
| 514 | Py_XDECREF(tmp); |
| 515 | return result; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 518 | PyDoc_STRVAR(eval_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 519 | "eval(source[, globals[, locals]]) -> value\n\ |
| 520 | \n\ |
| 521 | Evaluate the source in the context of globals and locals.\n\ |
| 522 | The source may be a string representing a Python expression\n\ |
| 523 | or a code object as returned by compile().\n\ |
| 524 | 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] | 525 | 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] | 526 | |
| 527 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 528 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 529 | builtin_execfile(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 530 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 531 | char *filename; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 532 | PyObject *globals = Py_None, *locals = Py_None; |
| 533 | PyObject *res; |
Guido van Rossum | b3a639e | 2001-09-05 13:37:47 +0000 | [diff] [blame] | 534 | FILE* fp = NULL; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 535 | PyCompilerFlags cf; |
Martin v. Löwis | 6b3a2c4 | 2001-08-08 05:30:36 +0000 | [diff] [blame] | 536 | int exists; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 537 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 538 | if (!PyArg_ParseTuple(args, "s|O!O!:execfile", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 539 | &filename, |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 540 | &PyDict_Type, &globals, |
| 541 | &PyDict_Type, &locals)) |
Guido van Rossum | 0f61f8a | 1992-02-25 18:55:05 +0000 | [diff] [blame] | 542 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 543 | if (globals == Py_None) { |
| 544 | globals = PyEval_GetGlobals(); |
| 545 | if (locals == Py_None) |
| 546 | locals = PyEval_GetLocals(); |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 547 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 548 | else if (locals == Py_None) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 549 | locals = globals; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 550 | if (PyDict_GetItemString(globals, "__builtins__") == NULL) { |
| 551 | if (PyDict_SetItemString(globals, "__builtins__", |
| 552 | PyEval_GetBuiltins()) != 0) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 553 | return NULL; |
| 554 | } |
Martin v. Löwis | 6b3a2c4 | 2001-08-08 05:30:36 +0000 | [diff] [blame] | 555 | |
| 556 | exists = 0; |
| 557 | /* Test for existence or directory. */ |
Martin v. Löwis | 3484a18 | 2002-03-09 12:07:51 +0000 | [diff] [blame] | 558 | #if defined(PLAN9) |
| 559 | { |
| 560 | Dir *d; |
| 561 | |
| 562 | if ((d = dirstat(filename))!=nil) { |
| 563 | if(d->mode & DMDIR) |
| 564 | werrstr("is a directory"); |
| 565 | else |
| 566 | exists = 1; |
| 567 | free(d); |
| 568 | } |
Martin v. Löwis | 6b3a2c4 | 2001-08-08 05:30:36 +0000 | [diff] [blame] | 569 | } |
Martin v. Löwis | 3484a18 | 2002-03-09 12:07:51 +0000 | [diff] [blame] | 570 | #elif defined(RISCOS) |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 571 | if (object_exists(filename)) { |
| 572 | if (isdir(filename)) |
| 573 | errno = EISDIR; |
| 574 | else |
| 575 | exists = 1; |
| 576 | } |
Martin v. Löwis | 3484a18 | 2002-03-09 12:07:51 +0000 | [diff] [blame] | 577 | #else /* standard Posix */ |
| 578 | { |
| 579 | struct stat s; |
| 580 | if (stat(filename, &s) == 0) { |
| 581 | if (S_ISDIR(s.st_mode)) |
| 582 | # if defined(PY_OS2) && defined(PYCC_VACPP) |
| 583 | errno = EOS2ERR; |
| 584 | # else |
| 585 | errno = EISDIR; |
| 586 | # endif |
| 587 | else |
| 588 | exists = 1; |
| 589 | } |
| 590 | } |
| 591 | #endif |
Martin v. Löwis | 6b3a2c4 | 2001-08-08 05:30:36 +0000 | [diff] [blame] | 592 | |
| 593 | if (exists) { |
| 594 | Py_BEGIN_ALLOW_THREADS |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 595 | fp = fopen(filename, "r" PY_STDIOTEXTMODE); |
Martin v. Löwis | 6b3a2c4 | 2001-08-08 05:30:36 +0000 | [diff] [blame] | 596 | Py_END_ALLOW_THREADS |
| 597 | |
| 598 | if (fp == NULL) { |
| 599 | exists = 0; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | if (!exists) { |
Peter Schneider-Kamp | 4c01342 | 2002-08-27 16:58:00 +0000 | [diff] [blame] | 604 | PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename); |
Guido van Rossum | 0f61f8a | 1992-02-25 18:55:05 +0000 | [diff] [blame] | 605 | return NULL; |
| 606 | } |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 607 | cf.cf_flags = 0; |
| 608 | if (PyEval_MergeCompilerFlags(&cf)) |
Tim Peters | 748b8bb | 2001-04-28 08:20:22 +0000 | [diff] [blame] | 609 | res = PyRun_FileExFlags(fp, filename, Py_file_input, globals, |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 610 | locals, 1, &cf); |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 611 | else |
Tim Peters | 748b8bb | 2001-04-28 08:20:22 +0000 | [diff] [blame] | 612 | res = PyRun_FileEx(fp, filename, Py_file_input, globals, |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 613 | locals, 1); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 614 | return res; |
Guido van Rossum | 0f61f8a | 1992-02-25 18:55:05 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 617 | PyDoc_STRVAR(execfile_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 618 | "execfile(filename[, globals[, locals]])\n\ |
| 619 | \n\ |
| 620 | Read and execute a Python script from a file.\n\ |
| 621 | 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] | 622 | 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] | 623 | |
| 624 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 625 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 626 | builtin_getattr(PyObject *self, PyObject *args) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 627 | { |
Guido van Rossum | 950ff29 | 1998-06-29 13:38:57 +0000 | [diff] [blame] | 628 | PyObject *v, *result, *dflt = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 629 | PyObject *name; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 630 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 631 | if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt)) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 632 | return NULL; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 633 | #ifdef Py_USING_UNICODE |
Jeremy Hylton | 0eb1115 | 2001-07-30 22:39:31 +0000 | [diff] [blame] | 634 | if (PyUnicode_Check(name)) { |
| 635 | name = _PyUnicode_AsDefaultEncodedString(name, NULL); |
| 636 | if (name == NULL) |
| 637 | return NULL; |
| 638 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 639 | #endif |
Jeremy Hylton | 0eb1115 | 2001-07-30 22:39:31 +0000 | [diff] [blame] | 640 | |
| 641 | if (!PyString_Check(name)) { |
| 642 | PyErr_SetString(PyExc_TypeError, |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 643 | "getattr(): attribute name must be string"); |
Jeremy Hylton | 0eb1115 | 2001-07-30 22:39:31 +0000 | [diff] [blame] | 644 | return NULL; |
| 645 | } |
Guido van Rossum | 950ff29 | 1998-06-29 13:38:57 +0000 | [diff] [blame] | 646 | result = PyObject_GetAttr(v, name); |
Guido van Rossum | d892357 | 2001-10-16 21:31:32 +0000 | [diff] [blame] | 647 | if (result == NULL && dflt != NULL && |
| 648 | PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 649 | { |
Guido van Rossum | 950ff29 | 1998-06-29 13:38:57 +0000 | [diff] [blame] | 650 | PyErr_Clear(); |
| 651 | Py_INCREF(dflt); |
| 652 | result = dflt; |
| 653 | } |
| 654 | return result; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 657 | PyDoc_STRVAR(getattr_doc, |
Guido van Rossum | 950ff29 | 1998-06-29 13:38:57 +0000 | [diff] [blame] | 658 | "getattr(object, name[, default]) -> value\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 659 | \n\ |
Guido van Rossum | 950ff29 | 1998-06-29 13:38:57 +0000 | [diff] [blame] | 660 | Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\ |
| 661 | 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] | 662 | exist; without it, an exception is raised in that case."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 663 | |
| 664 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 665 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 666 | builtin_globals(PyObject *self) |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 667 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 668 | PyObject *d; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 669 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 670 | d = PyEval_GetGlobals(); |
| 671 | Py_INCREF(d); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 672 | return d; |
| 673 | } |
| 674 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 675 | PyDoc_STRVAR(globals_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 676 | "globals() -> dictionary\n\ |
| 677 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 678 | Return the dictionary containing the current scope's global variables."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 679 | |
| 680 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 681 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 682 | builtin_hasattr(PyObject *self, PyObject *args) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 683 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 684 | PyObject *v; |
| 685 | PyObject *name; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 686 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 687 | if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name)) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 688 | return NULL; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 689 | #ifdef Py_USING_UNICODE |
Jeremy Hylton | 302b54a | 2001-07-30 22:45:19 +0000 | [diff] [blame] | 690 | if (PyUnicode_Check(name)) { |
| 691 | name = _PyUnicode_AsDefaultEncodedString(name, NULL); |
| 692 | if (name == NULL) |
| 693 | return NULL; |
| 694 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 695 | #endif |
Jeremy Hylton | 302b54a | 2001-07-30 22:45:19 +0000 | [diff] [blame] | 696 | |
| 697 | if (!PyString_Check(name)) { |
| 698 | PyErr_SetString(PyExc_TypeError, |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 699 | "hasattr(): attribute name must be string"); |
Jeremy Hylton | 302b54a | 2001-07-30 22:45:19 +0000 | [diff] [blame] | 700 | return NULL; |
| 701 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 702 | v = PyObject_GetAttr(v, name); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 703 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 704 | PyErr_Clear(); |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 705 | Py_INCREF(Py_False); |
| 706 | return Py_False; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 707 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 708 | Py_DECREF(v); |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 709 | Py_INCREF(Py_True); |
| 710 | return Py_True; |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 713 | PyDoc_STRVAR(hasattr_doc, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 714 | "hasattr(object, name) -> bool\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 715 | \n\ |
| 716 | 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] | 717 | (This is done by calling getattr(object, name) and catching exceptions.)"); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 718 | |
| 719 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 720 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 721 | builtin_id(PyObject *self, PyObject *v) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 722 | { |
Guido van Rossum | 106f2da | 2000-06-28 21:12:25 +0000 | [diff] [blame] | 723 | return PyLong_FromVoidPtr(v); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 726 | PyDoc_STRVAR(id_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 727 | "id(object) -> integer\n\ |
| 728 | \n\ |
| 729 | 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] | 730 | simultaneously existing objects. (Hint: it's the object's memory address.)"); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 731 | |
| 732 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 733 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 734 | builtin_map(PyObject *self, PyObject *args) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 735 | { |
| 736 | typedef struct { |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 737 | PyObject *it; /* the iterator object */ |
| 738 | int saw_StopIteration; /* bool: did the iterator end? */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 739 | } sequence; |
| 740 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 741 | PyObject *func, *result; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 742 | sequence *seqs = NULL, *sqp; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 743 | int n, len; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 744 | register int i, j; |
| 745 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 746 | n = PyTuple_Size(args); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 747 | if (n < 2) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 748 | PyErr_SetString(PyExc_TypeError, |
| 749 | "map() requires at least two args"); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 750 | return NULL; |
| 751 | } |
| 752 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 753 | func = PyTuple_GetItem(args, 0); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 754 | n--; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 755 | |
Guido van Rossum | fa4ac71 | 1998-07-10 17:37:30 +0000 | [diff] [blame] | 756 | if (func == Py_None && n == 1) { |
| 757 | /* map(None, S) is the same as list(S). */ |
| 758 | return PySequence_List(PyTuple_GetItem(args, 1)); |
| 759 | } |
| 760 | |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 761 | /* Get space for sequence descriptors. Must NULL out the iterator |
| 762 | * pointers so that jumping to Fail_2 later doesn't see trash. |
| 763 | */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 764 | if ((seqs = PyMem_NEW(sequence, n)) == NULL) { |
| 765 | PyErr_NoMemory(); |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 766 | return NULL; |
| 767 | } |
| 768 | for (i = 0; i < n; ++i) { |
| 769 | seqs[i].it = (PyObject*)NULL; |
| 770 | seqs[i].saw_StopIteration = 0; |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 771 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 772 | |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 773 | /* Do a first pass to obtain iterators for the arguments, and set len |
| 774 | * to the largest of their lengths. |
Tim Peters | 748b8bb | 2001-04-28 08:20:22 +0000 | [diff] [blame] | 775 | */ |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 776 | len = 0; |
| 777 | for (i = 0, sqp = seqs; i < n; ++i, ++sqp) { |
| 778 | PyObject *curseq; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 779 | int curlen; |
Guido van Rossum | ad99177 | 2001-01-12 16:03:05 +0000 | [diff] [blame] | 780 | |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 781 | /* Get iterator. */ |
| 782 | curseq = PyTuple_GetItem(args, i+1); |
| 783 | sqp->it = PyObject_GetIter(curseq); |
| 784 | if (sqp->it == NULL) { |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 785 | static char errmsg[] = |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 786 | "argument %d to map() must support iteration"; |
Guido van Rossum | 15e33a4 | 1997-04-30 19:00:27 +0000 | [diff] [blame] | 787 | char errbuf[sizeof(errmsg) + 25]; |
Jeremy Hylton | 518ab1c | 2001-11-28 20:42:20 +0000 | [diff] [blame] | 788 | PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 789 | PyErr_SetString(PyExc_TypeError, errbuf); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 790 | goto Fail_2; |
| 791 | } |
| 792 | |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 793 | /* Update len. */ |
| 794 | curlen = -1; /* unknown */ |
| 795 | if (PySequence_Check(curseq) && |
| 796 | curseq->ob_type->tp_as_sequence->sq_length) { |
| 797 | curlen = PySequence_Size(curseq); |
| 798 | if (curlen < 0) |
| 799 | PyErr_Clear(); |
| 800 | } |
Tim Peters | 748b8bb | 2001-04-28 08:20:22 +0000 | [diff] [blame] | 801 | if (curlen < 0) |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 802 | curlen = 8; /* arbitrary */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 803 | if (curlen > len) |
| 804 | len = curlen; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 807 | /* Get space for the result list. */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 808 | if ((result = (PyObject *) PyList_New(len)) == NULL) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 809 | goto Fail_2; |
| 810 | |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 811 | /* Iterate over the sequences until all have stopped. */ |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 812 | for (i = 0; ; ++i) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 813 | PyObject *alist, *item=NULL, *value; |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 814 | int numactive = 0; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 815 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 816 | if (func == Py_None && n == 1) |
Guido van Rossum | 3212031 | 1995-07-10 13:52:21 +0000 | [diff] [blame] | 817 | alist = NULL; |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 818 | else if ((alist = PyTuple_New(n)) == NULL) |
| 819 | goto Fail_1; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 820 | |
| 821 | for (j = 0, sqp = seqs; j < n; ++j, ++sqp) { |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 822 | if (sqp->saw_StopIteration) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 823 | Py_INCREF(Py_None); |
| 824 | item = Py_None; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 825 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 826 | else { |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 827 | item = PyIter_Next(sqp->it); |
| 828 | if (item) |
| 829 | ++numactive; |
| 830 | else { |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 831 | if (PyErr_Occurred()) { |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 832 | Py_XDECREF(alist); |
| 833 | goto Fail_1; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 834 | } |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 835 | Py_INCREF(Py_None); |
| 836 | item = Py_None; |
| 837 | sqp->saw_StopIteration = 1; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 838 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 839 | } |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 840 | if (alist) |
| 841 | PyTuple_SET_ITEM(alist, j, item); |
| 842 | else |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 843 | break; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Guido van Rossum | 3212031 | 1995-07-10 13:52:21 +0000 | [diff] [blame] | 846 | if (!alist) |
| 847 | alist = item; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 848 | |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 849 | if (numactive == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 850 | Py_DECREF(alist); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 851 | break; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 852 | } |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 853 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 854 | if (func == Py_None) |
Guido van Rossum | 3212031 | 1995-07-10 13:52:21 +0000 | [diff] [blame] | 855 | value = alist; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 856 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 857 | value = PyEval_CallObject(func, alist); |
| 858 | Py_DECREF(alist); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 859 | if (value == NULL) |
| 860 | goto Fail_1; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 861 | } |
| 862 | if (i >= len) { |
Barry Warsaw | fa77e09 | 1999-01-28 18:49:12 +0000 | [diff] [blame] | 863 | int status = PyList_Append(result, value); |
Barry Warsaw | 2133287 | 1999-01-28 04:21:35 +0000 | [diff] [blame] | 864 | Py_DECREF(value); |
Barry Warsaw | fa77e09 | 1999-01-28 18:49:12 +0000 | [diff] [blame] | 865 | if (status < 0) |
| 866 | goto Fail_1; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 867 | } |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 868 | else if (PyList_SetItem(result, i, value) < 0) |
| 869 | goto Fail_1; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Guido van Rossum | fa4ac71 | 1998-07-10 17:37:30 +0000 | [diff] [blame] | 872 | if (i < len && PyList_SetSlice(result, i, len, NULL) < 0) |
| 873 | goto Fail_1; |
| 874 | |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 875 | goto Succeed; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 876 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 877 | Fail_1: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 878 | Py_DECREF(result); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 879 | Fail_2: |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 880 | result = NULL; |
| 881 | Succeed: |
| 882 | assert(seqs); |
| 883 | for (i = 0; i < n; ++i) |
| 884 | Py_XDECREF(seqs[i].it); |
| 885 | PyMem_DEL(seqs); |
| 886 | return result; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 889 | PyDoc_STRVAR(map_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 890 | "map(function, sequence[, sequence, ...]) -> list\n\ |
| 891 | \n\ |
| 892 | Return a list of the results of applying the function to the items of\n\ |
| 893 | the argument sequence(s). If more than one sequence is given, the\n\ |
| 894 | function is called with an argument list consisting of the corresponding\n\ |
| 895 | item of each sequence, substituting None for missing values when not all\n\ |
| 896 | 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] | 897 | 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] | 898 | |
| 899 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 900 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 901 | builtin_setattr(PyObject *self, PyObject *args) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 902 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 903 | PyObject *v; |
| 904 | PyObject *name; |
| 905 | PyObject *value; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 906 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 907 | if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value)) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 908 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 909 | if (PyObject_SetAttr(v, name, value) != 0) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 910 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 911 | Py_INCREF(Py_None); |
| 912 | return Py_None; |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 913 | } |
| 914 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 915 | PyDoc_STRVAR(setattr_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 916 | "setattr(object, name, value)\n\ |
| 917 | \n\ |
| 918 | 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] | 919 | ``x.y = v''."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 920 | |
| 921 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 922 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 923 | builtin_delattr(PyObject *self, PyObject *args) |
Guido van Rossum | 14144fc | 1994-08-29 12:53:40 +0000 | [diff] [blame] | 924 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 925 | PyObject *v; |
| 926 | PyObject *name; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 927 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 928 | if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name)) |
Guido van Rossum | 14144fc | 1994-08-29 12:53:40 +0000 | [diff] [blame] | 929 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 930 | if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0) |
Guido van Rossum | 14144fc | 1994-08-29 12:53:40 +0000 | [diff] [blame] | 931 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 932 | Py_INCREF(Py_None); |
| 933 | return Py_None; |
Guido van Rossum | 14144fc | 1994-08-29 12:53:40 +0000 | [diff] [blame] | 934 | } |
| 935 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 936 | PyDoc_STRVAR(delattr_doc, |
Guido van Rossum | df12a59 | 1998-11-23 22:13:04 +0000 | [diff] [blame] | 937 | "delattr(object, name)\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 938 | \n\ |
| 939 | 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] | 940 | ``del x.y''."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 941 | |
| 942 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 943 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 944 | builtin_hash(PyObject *self, PyObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 945 | { |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 946 | long x; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 947 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 948 | x = PyObject_Hash(v); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 949 | if (x == -1) |
| 950 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 951 | return PyInt_FromLong(x); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 952 | } |
| 953 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 954 | PyDoc_STRVAR(hash_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 955 | "hash(object) -> integer\n\ |
| 956 | \n\ |
| 957 | 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] | 958 | 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] | 959 | |
| 960 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 961 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 962 | builtin_hex(PyObject *self, PyObject *v) |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 963 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 964 | PyNumberMethods *nb; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 965 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 966 | if ((nb = v->ob_type->tp_as_number) == NULL || |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 967 | nb->nb_hex == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 968 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 969 | "hex() argument can't be converted to hex"); |
| 970 | return NULL; |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 971 | } |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 972 | return (*nb->nb_hex)(v); |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 973 | } |
| 974 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 975 | PyDoc_STRVAR(hex_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 976 | "hex(number) -> string\n\ |
| 977 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 978 | Return the hexadecimal representation of an integer or long integer."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 979 | |
| 980 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 981 | static PyObject *builtin_raw_input(PyObject *, PyObject *); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 982 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 983 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 984 | builtin_input(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 985 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 986 | PyObject *line; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 987 | char *str; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 988 | PyObject *res; |
| 989 | PyObject *globals, *locals; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 990 | |
| 991 | line = builtin_raw_input(self, args); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 992 | if (line == NULL) |
| 993 | return line; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 994 | if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 995 | return NULL; |
| 996 | while (*str == ' ' || *str == '\t') |
| 997 | str++; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 998 | globals = PyEval_GetGlobals(); |
| 999 | locals = PyEval_GetLocals(); |
| 1000 | if (PyDict_GetItemString(globals, "__builtins__") == NULL) { |
| 1001 | if (PyDict_SetItemString(globals, "__builtins__", |
| 1002 | PyEval_GetBuiltins()) != 0) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 1003 | return NULL; |
| 1004 | } |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 1005 | res = PyRun_String(str, Py_eval_input, globals, locals); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1006 | Py_DECREF(line); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1007 | return res; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1010 | PyDoc_STRVAR(input_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1011 | "input([prompt]) -> value\n\ |
| 1012 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1013 | Equivalent to eval(raw_input(prompt))."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1014 | |
| 1015 | |
Guido van Rossum | e8811f8 | 1997-02-14 15:48:05 +0000 | [diff] [blame] | 1016 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1017 | builtin_intern(PyObject *self, PyObject *args) |
Guido van Rossum | e8811f8 | 1997-02-14 15:48:05 +0000 | [diff] [blame] | 1018 | { |
| 1019 | PyObject *s; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1020 | if (!PyArg_ParseTuple(args, "S:intern", &s)) |
Guido van Rossum | e8811f8 | 1997-02-14 15:48:05 +0000 | [diff] [blame] | 1021 | return NULL; |
| 1022 | Py_INCREF(s); |
| 1023 | PyString_InternInPlace(&s); |
| 1024 | return s; |
| 1025 | } |
| 1026 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1027 | PyDoc_STRVAR(intern_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1028 | "intern(string) -> string\n\ |
| 1029 | \n\ |
| 1030 | ``Intern'' the given string. This enters the string in the (global)\n\ |
| 1031 | table of interned strings whose purpose is to speed up dictionary lookups.\n\ |
| 1032 | 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] | 1033 | same value."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1034 | |
| 1035 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1036 | static PyObject * |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1037 | builtin_iter(PyObject *self, PyObject *args) |
| 1038 | { |
| 1039 | PyObject *v, *w = NULL; |
| 1040 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1041 | if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w)) |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1042 | return NULL; |
| 1043 | if (w == NULL) |
| 1044 | return PyObject_GetIter(v); |
| 1045 | if (!PyCallable_Check(v)) { |
| 1046 | PyErr_SetString(PyExc_TypeError, |
| 1047 | "iter(v, w): v must be callable"); |
| 1048 | return NULL; |
| 1049 | } |
| 1050 | return PyCallIter_New(v, w); |
| 1051 | } |
| 1052 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1053 | PyDoc_STRVAR(iter_doc, |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1054 | "iter(collection) -> iterator\n\ |
| 1055 | iter(callable, sentinel) -> iterator\n\ |
| 1056 | \n\ |
| 1057 | Get an iterator from an object. In the first form, the argument must\n\ |
| 1058 | supply its own iterator, or be a sequence.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1059 | 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] | 1060 | |
| 1061 | |
| 1062 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1063 | builtin_len(PyObject *self, PyObject *v) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1064 | { |
Guido van Rossum | 8ea9f4d | 1998-06-29 22:26:50 +0000 | [diff] [blame] | 1065 | long res; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1066 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1067 | res = PyObject_Size(v); |
Guido van Rossum | 8ea9f4d | 1998-06-29 22:26:50 +0000 | [diff] [blame] | 1068 | if (res < 0 && PyErr_Occurred()) |
| 1069 | return NULL; |
| 1070 | return PyInt_FromLong(res); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1073 | PyDoc_STRVAR(len_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1074 | "len(object) -> integer\n\ |
| 1075 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1076 | Return the number of items of a sequence or mapping."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1077 | |
| 1078 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1079 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1080 | builtin_locals(PyObject *self) |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1081 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1082 | PyObject *d; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1083 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1084 | d = PyEval_GetLocals(); |
| 1085 | Py_INCREF(d); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1086 | return d; |
| 1087 | } |
| 1088 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1089 | PyDoc_STRVAR(locals_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1090 | "locals() -> dictionary\n\ |
| 1091 | \n\ |
Raymond Hettinger | 69bf8f3 | 2003-01-04 02:16:22 +0000 | [diff] [blame] | 1092 | 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] | 1093 | |
| 1094 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1095 | static PyObject * |
Guido van Rossum | 53451b3 | 2001-01-17 15:47:24 +0000 | [diff] [blame] | 1096 | min_max(PyObject *args, int op) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1097 | { |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 1098 | PyObject *v, *w, *x, *it; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1099 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1100 | if (PyTuple_Size(args) > 1) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1101 | v = args; |
Raymond Hettinger | bbfb910 | 2002-12-29 18:31:19 +0000 | [diff] [blame] | 1102 | else if (!PyArg_UnpackTuple(args, (op==Py_LT) ? "min" : "max", 1, 1, &v)) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1103 | return NULL; |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1104 | |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 1105 | it = PyObject_GetIter(v); |
| 1106 | if (it == NULL) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1107 | return NULL; |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 1108 | |
| 1109 | w = NULL; /* the result */ |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 1110 | for (;;) { |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 1111 | x = PyIter_Next(it); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1112 | if (x == NULL) { |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 1113 | if (PyErr_Occurred()) { |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 1114 | Py_XDECREF(w); |
| 1115 | Py_DECREF(it); |
| 1116 | return NULL; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1117 | } |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 1118 | break; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1119 | } |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 1120 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1121 | if (w == NULL) |
| 1122 | w = x; |
| 1123 | else { |
Guido van Rossum | 53451b3 | 2001-01-17 15:47:24 +0000 | [diff] [blame] | 1124 | int cmp = PyObject_RichCompareBool(x, w, op); |
| 1125 | if (cmp > 0) { |
| 1126 | Py_DECREF(w); |
| 1127 | w = x; |
| 1128 | } |
| 1129 | else if (cmp < 0) { |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 1130 | Py_DECREF(x); |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 1131 | Py_DECREF(w); |
| 1132 | Py_DECREF(it); |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 1133 | return NULL; |
| 1134 | } |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1135 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1136 | Py_DECREF(x); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1137 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1138 | } |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1139 | if (w == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1140 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1141 | "min() or max() arg is an empty sequence"); |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 1142 | Py_DECREF(it); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1143 | return w; |
| 1144 | } |
| 1145 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1146 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1147 | builtin_min(PyObject *self, PyObject *v) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1148 | { |
Guido van Rossum | 53451b3 | 2001-01-17 15:47:24 +0000 | [diff] [blame] | 1149 | return min_max(v, Py_LT); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1152 | PyDoc_STRVAR(min_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1153 | "min(sequence) -> value\n\ |
| 1154 | min(a, b, c, ...) -> value\n\ |
| 1155 | \n\ |
| 1156 | With a single sequence argument, return its smallest item.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1157 | With two or more arguments, return the smallest argument."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1158 | |
| 1159 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1160 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1161 | builtin_max(PyObject *self, PyObject *v) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1162 | { |
Guido van Rossum | 53451b3 | 2001-01-17 15:47:24 +0000 | [diff] [blame] | 1163 | return min_max(v, Py_GT); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1166 | PyDoc_STRVAR(max_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1167 | "max(sequence) -> value\n\ |
| 1168 | max(a, b, c, ...) -> value\n\ |
| 1169 | \n\ |
| 1170 | With a single sequence argument, return its largest item.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1171 | With two or more arguments, return the largest argument."); |
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_oct(PyObject *self, PyObject *v) |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 1176 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1177 | PyNumberMethods *nb; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1178 | |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1179 | if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL || |
| 1180 | nb->nb_oct == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1181 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1182 | "oct() argument can't be converted to oct"); |
| 1183 | return NULL; |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 1184 | } |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1185 | return (*nb->nb_oct)(v); |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1188 | PyDoc_STRVAR(oct_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1189 | "oct(number) -> string\n\ |
| 1190 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1191 | Return the octal representation of an integer or long integer."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1192 | |
| 1193 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1194 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1195 | builtin_ord(PyObject *self, PyObject* obj) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1196 | { |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 1197 | long ord; |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1198 | int size; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1199 | |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1200 | if (PyString_Check(obj)) { |
| 1201 | size = PyString_GET_SIZE(obj); |
Andrew M. Kuchling | 34c20cf | 2000-12-20 14:36:56 +0000 | [diff] [blame] | 1202 | if (size == 1) { |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1203 | ord = (long)((unsigned char)*PyString_AS_STRING(obj)); |
Andrew M. Kuchling | 34c20cf | 2000-12-20 14:36:56 +0000 | [diff] [blame] | 1204 | return PyInt_FromLong(ord); |
| 1205 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1206 | #ifdef Py_USING_UNICODE |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1207 | } else if (PyUnicode_Check(obj)) { |
| 1208 | size = PyUnicode_GET_SIZE(obj); |
Andrew M. Kuchling | 34c20cf | 2000-12-20 14:36:56 +0000 | [diff] [blame] | 1209 | if (size == 1) { |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1210 | ord = (long)*PyUnicode_AS_UNICODE(obj); |
Andrew M. Kuchling | 34c20cf | 2000-12-20 14:36:56 +0000 | [diff] [blame] | 1211 | return PyInt_FromLong(ord); |
| 1212 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1213 | #endif |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1214 | } else { |
| 1215 | PyErr_Format(PyExc_TypeError, |
Andrew M. Kuchling | f07aad1 | 2000-12-23 14:11:28 +0000 | [diff] [blame] | 1216 | "ord() expected string of length 1, but " \ |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1217 | "%.200s found", obj->ob_type->tp_name); |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 1218 | return NULL; |
| 1219 | } |
| 1220 | |
Guido van Rossum | ad99177 | 2001-01-12 16:03:05 +0000 | [diff] [blame] | 1221 | PyErr_Format(PyExc_TypeError, |
Andrew M. Kuchling | f07aad1 | 2000-12-23 14:11:28 +0000 | [diff] [blame] | 1222 | "ord() expected a character, " |
| 1223 | "but string of length %d found", |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1224 | size); |
| 1225 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1228 | PyDoc_STRVAR(ord_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1229 | "ord(c) -> integer\n\ |
| 1230 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1231 | Return the integer ordinal of a one-character string."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1232 | |
| 1233 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1234 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1235 | builtin_pow(PyObject *self, PyObject *args) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1236 | { |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 1237 | PyObject *v, *w, *z = Py_None; |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1238 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1239 | if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z)) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1240 | return NULL; |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 1241 | return PyNumber_Power(v, w, z); |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1244 | PyDoc_STRVAR(pow_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1245 | "pow(x, y[, z]) -> number\n\ |
| 1246 | \n\ |
| 1247 | 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] | 1248 | 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] | 1249 | |
| 1250 | |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1251 | |
| 1252 | /* Return number of items in range (lo, hi, step), when arguments are |
| 1253 | * PyInt or PyLong objects. step > 0 required. Return a value < 0 if |
| 1254 | * & only if the true value is too large to fit in a signed long. |
| 1255 | * Arguments MUST return 1 with either PyInt_Check() or |
| 1256 | * PyLong_Check(). Return -1 when there is an error. |
| 1257 | */ |
| 1258 | static long |
| 1259 | get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step) |
| 1260 | { |
| 1261 | /* ------------------------------------------------------------- |
| 1262 | Algorithm is equal to that of get_len_of_range(), but it operates |
| 1263 | on PyObjects (which are assumed to be PyLong or PyInt objects). |
| 1264 | ---------------------------------------------------------------*/ |
| 1265 | long n; |
| 1266 | PyObject *diff = NULL; |
| 1267 | PyObject *one = NULL; |
| 1268 | PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL; |
| 1269 | /* holds sub-expression evaluations */ |
| 1270 | |
| 1271 | /* if (lo >= hi), return length of 0. */ |
| 1272 | if (PyObject_Compare(lo, hi) >= 0) |
| 1273 | return 0; |
| 1274 | |
| 1275 | if ((one = PyLong_FromLong(1L)) == NULL) |
| 1276 | goto Fail; |
| 1277 | |
| 1278 | if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL) |
| 1279 | goto Fail; |
| 1280 | |
| 1281 | if ((diff = PyNumber_Subtract(tmp1, one)) == NULL) |
| 1282 | goto Fail; |
| 1283 | |
| 1284 | if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL) |
| 1285 | goto Fail; |
| 1286 | |
| 1287 | if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL) |
| 1288 | goto Fail; |
| 1289 | |
| 1290 | n = PyLong_AsLong(tmp3); |
| 1291 | if (PyErr_Occurred()) { /* Check for Overflow */ |
| 1292 | PyErr_Clear(); |
| 1293 | goto Fail; |
| 1294 | } |
| 1295 | |
| 1296 | Py_DECREF(tmp3); |
| 1297 | Py_DECREF(tmp2); |
| 1298 | Py_DECREF(diff); |
| 1299 | Py_DECREF(tmp1); |
| 1300 | Py_DECREF(one); |
| 1301 | return n; |
| 1302 | |
| 1303 | Fail: |
| 1304 | Py_XDECREF(tmp3); |
| 1305 | Py_XDECREF(tmp2); |
| 1306 | Py_XDECREF(diff); |
| 1307 | Py_XDECREF(tmp1); |
| 1308 | Py_XDECREF(one); |
| 1309 | return -1; |
| 1310 | } |
| 1311 | |
| 1312 | /* An extension of builtin_range() that handles the case when PyLong |
| 1313 | * arguments are given. */ |
| 1314 | static PyObject * |
| 1315 | handle_range_longs(PyObject *self, PyObject *args) |
| 1316 | { |
| 1317 | PyObject *ilow; |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1318 | PyObject *ihigh = NULL; |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1319 | PyObject *istep = NULL; |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1320 | |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1321 | PyObject *curnum = NULL; |
| 1322 | PyObject *v = NULL; |
| 1323 | long bign; |
| 1324 | int i, n; |
| 1325 | int cmp_result; |
| 1326 | |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1327 | PyObject *zero = PyLong_FromLong(0); |
| 1328 | |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1329 | if (zero == NULL) |
| 1330 | return NULL; |
| 1331 | |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1332 | if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) { |
| 1333 | Py_DECREF(zero); |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1334 | return NULL; |
| 1335 | } |
| 1336 | |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1337 | /* Figure out which way we were called, supply defaults, and be |
| 1338 | * sure to incref everything so that the decrefs at the end |
| 1339 | * are correct. |
| 1340 | */ |
| 1341 | assert(ilow != NULL); |
| 1342 | if (ihigh == NULL) { |
| 1343 | /* only 1 arg -- it's the upper limit */ |
| 1344 | ihigh = ilow; |
| 1345 | ilow = NULL; |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1346 | } |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1347 | assert(ihigh != NULL); |
| 1348 | Py_INCREF(ihigh); |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1349 | |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1350 | /* ihigh correct now; do ilow */ |
| 1351 | if (ilow == NULL) |
| 1352 | ilow = zero; |
| 1353 | Py_INCREF(ilow); |
| 1354 | |
| 1355 | /* ilow and ihigh correct now; do istep */ |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1356 | if (istep == NULL) { |
| 1357 | istep = PyLong_FromLong(1L); |
| 1358 | if (istep == NULL) |
| 1359 | goto Fail; |
| 1360 | } |
| 1361 | else { |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1362 | Py_INCREF(istep); |
| 1363 | } |
| 1364 | |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1365 | if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) { |
Guido van Rossum | 28e83e3 | 2003-04-15 12:43:26 +0000 | [diff] [blame] | 1366 | PyErr_Format(PyExc_TypeError, |
Alex Martelli | f471d47 | 2003-04-23 13:00:44 +0000 | [diff] [blame] | 1367 | "range() integer start argument expected, got %s.", |
Guido van Rossum | 817d6c9 | 2003-04-14 18:25:04 +0000 | [diff] [blame] | 1368 | ilow->ob_type->tp_name); |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1369 | goto Fail; |
| 1370 | } |
| 1371 | |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1372 | if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) { |
Guido van Rossum | 28e83e3 | 2003-04-15 12:43:26 +0000 | [diff] [blame] | 1373 | PyErr_Format(PyExc_TypeError, |
Alex Martelli | f471d47 | 2003-04-23 13:00:44 +0000 | [diff] [blame] | 1374 | "range() integer end argument expected, got %s.", |
Guido van Rossum | 817d6c9 | 2003-04-14 18:25:04 +0000 | [diff] [blame] | 1375 | ihigh->ob_type->tp_name); |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1376 | goto Fail; |
| 1377 | } |
| 1378 | |
| 1379 | if (!PyInt_Check(istep) && !PyLong_Check(istep)) { |
Guido van Rossum | 28e83e3 | 2003-04-15 12:43:26 +0000 | [diff] [blame] | 1380 | PyErr_Format(PyExc_TypeError, |
Alex Martelli | f471d47 | 2003-04-23 13:00:44 +0000 | [diff] [blame] | 1381 | "range() integer step argument expected, got %s.", |
Guido van Rossum | 817d6c9 | 2003-04-14 18:25:04 +0000 | [diff] [blame] | 1382 | istep->ob_type->tp_name); |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1383 | goto Fail; |
| 1384 | } |
| 1385 | |
| 1386 | if (PyObject_Cmp(istep, zero, &cmp_result) == -1) |
| 1387 | goto Fail; |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1388 | if (cmp_result == 0) { |
| 1389 | PyErr_SetString(PyExc_ValueError, |
Alex Martelli | f471d47 | 2003-04-23 13:00:44 +0000 | [diff] [blame] | 1390 | "range() step argument must not be zero"); |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1391 | goto Fail; |
| 1392 | } |
| 1393 | |
| 1394 | if (cmp_result > 0) |
| 1395 | bign = get_len_of_range_longs(ilow, ihigh, istep); |
| 1396 | else { |
| 1397 | PyObject *neg_istep = PyNumber_Negative(istep); |
| 1398 | if (neg_istep == NULL) |
| 1399 | goto Fail; |
| 1400 | bign = get_len_of_range_longs(ihigh, ilow, neg_istep); |
| 1401 | Py_DECREF(neg_istep); |
| 1402 | } |
| 1403 | |
| 1404 | n = (int)bign; |
| 1405 | if (bign < 0 || (long)n != bign) { |
| 1406 | PyErr_SetString(PyExc_OverflowError, |
| 1407 | "range() result has too many items"); |
| 1408 | goto Fail; |
| 1409 | } |
| 1410 | |
| 1411 | v = PyList_New(n); |
| 1412 | if (v == NULL) |
| 1413 | goto Fail; |
| 1414 | |
| 1415 | curnum = ilow; |
| 1416 | Py_INCREF(curnum); |
| 1417 | |
| 1418 | for (i = 0; i < n; i++) { |
| 1419 | PyObject *w = PyNumber_Long(curnum); |
| 1420 | PyObject *tmp_num; |
| 1421 | if (w == NULL) |
| 1422 | goto Fail; |
| 1423 | |
| 1424 | PyList_SET_ITEM(v, i, w); |
| 1425 | |
| 1426 | tmp_num = PyNumber_Add(curnum, istep); |
| 1427 | if (tmp_num == NULL) |
| 1428 | goto Fail; |
| 1429 | |
| 1430 | Py_DECREF(curnum); |
| 1431 | curnum = tmp_num; |
| 1432 | } |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1433 | Py_DECREF(ilow); |
| 1434 | Py_DECREF(ihigh); |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1435 | Py_DECREF(istep); |
| 1436 | Py_DECREF(zero); |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1437 | Py_DECREF(curnum); |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1438 | return v; |
| 1439 | |
| 1440 | Fail: |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1441 | Py_DECREF(ilow); |
| 1442 | Py_DECREF(ihigh); |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1443 | Py_XDECREF(istep); |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1444 | Py_DECREF(zero); |
| 1445 | Py_XDECREF(curnum); |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1446 | Py_XDECREF(v); |
| 1447 | return NULL; |
| 1448 | } |
| 1449 | |
Guido van Rossum | 124eff0 | 1999-02-23 16:11:01 +0000 | [diff] [blame] | 1450 | /* Return number of items in range/xrange (lo, hi, step). step > 0 |
| 1451 | * required. Return a value < 0 if & only if the true value is too |
| 1452 | * large to fit in a signed long. |
| 1453 | */ |
| 1454 | static long |
Thomas Wouters | e28c296 | 2000-07-23 22:21:32 +0000 | [diff] [blame] | 1455 | get_len_of_range(long lo, long hi, long step) |
Guido van Rossum | 124eff0 | 1999-02-23 16:11:01 +0000 | [diff] [blame] | 1456 | { |
| 1457 | /* ------------------------------------------------------------- |
| 1458 | If lo >= hi, the range is empty. |
| 1459 | Else if n values are in the range, the last one is |
| 1460 | lo + (n-1)*step, which must be <= hi-1. Rearranging, |
| 1461 | n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives |
| 1462 | the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so |
| 1463 | the RHS is non-negative and so truncation is the same as the |
| 1464 | floor. Letting M be the largest positive long, the worst case |
| 1465 | for the RHS numerator is hi=M, lo=-M-1, and then |
| 1466 | hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough |
| 1467 | precision to compute the RHS exactly. |
| 1468 | ---------------------------------------------------------------*/ |
| 1469 | long n = 0; |
| 1470 | if (lo < hi) { |
| 1471 | unsigned long uhi = (unsigned long)hi; |
| 1472 | unsigned long ulo = (unsigned long)lo; |
| 1473 | unsigned long diff = uhi - ulo - 1; |
| 1474 | n = (long)(diff / (unsigned long)step + 1); |
| 1475 | } |
| 1476 | return n; |
| 1477 | } |
| 1478 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1479 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1480 | builtin_range(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1481 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1482 | long ilow = 0, ihigh = 0, istep = 1; |
Guido van Rossum | 124eff0 | 1999-02-23 16:11:01 +0000 | [diff] [blame] | 1483 | long bign; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1484 | int i, n; |
Guido van Rossum | 124eff0 | 1999-02-23 16:11:01 +0000 | [diff] [blame] | 1485 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1486 | PyObject *v; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1487 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1488 | if (PyTuple_Size(args) <= 1) { |
| 1489 | if (!PyArg_ParseTuple(args, |
Guido van Rossum | 0865dd9 | 1995-01-17 16:30:22 +0000 | [diff] [blame] | 1490 | "l;range() requires 1-3 int arguments", |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1491 | &ihigh)) { |
| 1492 | PyErr_Clear(); |
| 1493 | return handle_range_longs(self, args); |
| 1494 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1495 | } |
| 1496 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1497 | if (!PyArg_ParseTuple(args, |
Guido van Rossum | 0865dd9 | 1995-01-17 16:30:22 +0000 | [diff] [blame] | 1498 | "ll|l;range() requires 1-3 int arguments", |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1499 | &ilow, &ihigh, &istep)) { |
| 1500 | PyErr_Clear(); |
| 1501 | return handle_range_longs(self, args); |
| 1502 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1503 | } |
| 1504 | if (istep == 0) { |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1505 | PyErr_SetString(PyExc_ValueError, |
Alex Martelli | f471d47 | 2003-04-23 13:00:44 +0000 | [diff] [blame] | 1506 | "range() step argument must not be zero"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1507 | return NULL; |
| 1508 | } |
Guido van Rossum | 124eff0 | 1999-02-23 16:11:01 +0000 | [diff] [blame] | 1509 | if (istep > 0) |
| 1510 | bign = get_len_of_range(ilow, ihigh, istep); |
| 1511 | else |
| 1512 | bign = get_len_of_range(ihigh, ilow, -istep); |
| 1513 | n = (int)bign; |
| 1514 | if (bign < 0 || (long)n != bign) { |
Guido van Rossum | e23cde2 | 1999-01-12 05:07:47 +0000 | [diff] [blame] | 1515 | PyErr_SetString(PyExc_OverflowError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1516 | "range() result has too many items"); |
Guido van Rossum | e23cde2 | 1999-01-12 05:07:47 +0000 | [diff] [blame] | 1517 | return NULL; |
| 1518 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1519 | v = PyList_New(n); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1520 | if (v == NULL) |
| 1521 | return NULL; |
| 1522 | for (i = 0; i < n; i++) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1523 | PyObject *w = PyInt_FromLong(ilow); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1524 | if (w == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1525 | Py_DECREF(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1526 | return NULL; |
| 1527 | } |
Guido van Rossum | a937d14 | 1998-04-24 18:22:02 +0000 | [diff] [blame] | 1528 | PyList_SET_ITEM(v, i, w); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1529 | ilow += istep; |
| 1530 | } |
| 1531 | return v; |
| 1532 | } |
| 1533 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1534 | PyDoc_STRVAR(range_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1535 | "range([start,] stop[, step]) -> list of integers\n\ |
| 1536 | \n\ |
| 1537 | Return a list containing an arithmetic progression of integers.\n\ |
| 1538 | range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\ |
| 1539 | When step is given, it specifies the increment (or decrement).\n\ |
| 1540 | 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] | 1541 | 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] | 1542 | |
| 1543 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1544 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1545 | builtin_raw_input(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1546 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1547 | PyObject *v = NULL; |
Martin v. Löwis | 31d2df5 | 2002-08-14 15:46:02 +0000 | [diff] [blame] | 1548 | PyObject *fin = PySys_GetObject("stdin"); |
| 1549 | PyObject *fout = PySys_GetObject("stdout"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1550 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1551 | if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v)) |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1552 | return NULL; |
Martin v. Löwis | 31d2df5 | 2002-08-14 15:46:02 +0000 | [diff] [blame] | 1553 | |
| 1554 | if (fin == NULL) { |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 1555 | PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin"); |
Martin v. Löwis | 31d2df5 | 2002-08-14 15:46:02 +0000 | [diff] [blame] | 1556 | return NULL; |
| 1557 | } |
| 1558 | if (fout == NULL) { |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 1559 | PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout"); |
Martin v. Löwis | 31d2df5 | 2002-08-14 15:46:02 +0000 | [diff] [blame] | 1560 | return NULL; |
| 1561 | } |
| 1562 | if (PyFile_SoftSpace(fout, 0)) { |
| 1563 | if (PyFile_WriteString(" ", fout) != 0) |
| 1564 | return NULL; |
| 1565 | } |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 1566 | if (PyFile_Check (fin) && PyFile_Check (fout) |
| 1567 | && isatty(fileno(PyFile_AsFile(fin))) |
| 1568 | && isatty(fileno(PyFile_AsFile(fout)))) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1569 | PyObject *po; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1570 | char *prompt; |
| 1571 | char *s; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1572 | PyObject *result; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1573 | if (v != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1574 | po = PyObject_Str(v); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1575 | if (po == NULL) |
| 1576 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1577 | prompt = PyString_AsString(po); |
Guido van Rossum | d9b5208 | 1998-06-26 18:25:38 +0000 | [diff] [blame] | 1578 | if (prompt == NULL) |
| 1579 | return NULL; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1580 | } |
| 1581 | else { |
| 1582 | po = NULL; |
| 1583 | prompt = ""; |
| 1584 | } |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 1585 | s = PyOS_Readline(PyFile_AsFile (fin), PyFile_AsFile (fout), |
| 1586 | prompt); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1587 | Py_XDECREF(po); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1588 | if (s == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1589 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1590 | return NULL; |
| 1591 | } |
| 1592 | if (*s == '\0') { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1593 | PyErr_SetNone(PyExc_EOFError); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1594 | result = NULL; |
| 1595 | } |
| 1596 | else { /* strip trailing '\n' */ |
Guido van Rossum | 106f2da | 2000-06-28 21:12:25 +0000 | [diff] [blame] | 1597 | size_t len = strlen(s); |
| 1598 | if (len > INT_MAX) { |
Martin v. Löwis | 31d2df5 | 2002-08-14 15:46:02 +0000 | [diff] [blame] | 1599 | PyErr_SetString(PyExc_OverflowError, |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 1600 | "[raw_]input: input too long"); |
Guido van Rossum | 106f2da | 2000-06-28 21:12:25 +0000 | [diff] [blame] | 1601 | result = NULL; |
| 1602 | } |
| 1603 | else { |
Martin v. Löwis | 31d2df5 | 2002-08-14 15:46:02 +0000 | [diff] [blame] | 1604 | result = PyString_FromStringAndSize(s, |
| 1605 | (int)(len-1)); |
Guido van Rossum | 106f2da | 2000-06-28 21:12:25 +0000 | [diff] [blame] | 1606 | } |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1607 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1608 | PyMem_FREE(s); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1609 | return result; |
| 1610 | } |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 1611 | if (v != NULL) { |
Martin v. Löwis | 31d2df5 | 2002-08-14 15:46:02 +0000 | [diff] [blame] | 1612 | if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0) |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 1613 | return NULL; |
| 1614 | } |
Martin v. Löwis | 31d2df5 | 2002-08-14 15:46:02 +0000 | [diff] [blame] | 1615 | return PyFile_GetLine(fin, -1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1618 | PyDoc_STRVAR(raw_input_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1619 | "raw_input([prompt]) -> string\n\ |
| 1620 | \n\ |
| 1621 | Read a string from standard input. The trailing newline is stripped.\n\ |
| 1622 | If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\ |
| 1623 | 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] | 1624 | is printed without a trailing newline before reading."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1625 | |
| 1626 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1627 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1628 | builtin_reduce(PyObject *self, PyObject *args) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1629 | { |
Tim Peters | 15d81ef | 2001-05-04 04:39:21 +0000 | [diff] [blame] | 1630 | PyObject *seq, *func, *result = NULL, *it; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1631 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1632 | if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1633 | return NULL; |
| 1634 | if (result != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1635 | Py_INCREF(result); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1636 | |
Tim Peters | 15d81ef | 2001-05-04 04:39:21 +0000 | [diff] [blame] | 1637 | it = PyObject_GetIter(seq); |
| 1638 | if (it == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1639 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | 15d81ef | 2001-05-04 04:39:21 +0000 | [diff] [blame] | 1640 | "reduce() arg 2 must support iteration"); |
| 1641 | Py_XDECREF(result); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1642 | return NULL; |
| 1643 | } |
| 1644 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1645 | if ((args = PyTuple_New(2)) == NULL) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1646 | goto Fail; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1647 | |
Tim Peters | 15d81ef | 2001-05-04 04:39:21 +0000 | [diff] [blame] | 1648 | for (;;) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1649 | PyObject *op2; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1650 | |
| 1651 | if (args->ob_refcnt > 1) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1652 | Py_DECREF(args); |
| 1653 | if ((args = PyTuple_New(2)) == NULL) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1654 | goto Fail; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
Tim Peters | 15d81ef | 2001-05-04 04:39:21 +0000 | [diff] [blame] | 1657 | op2 = PyIter_Next(it); |
| 1658 | if (op2 == NULL) { |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 1659 | if (PyErr_Occurred()) |
| 1660 | goto Fail; |
| 1661 | break; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1662 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1663 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1664 | if (result == NULL) |
| 1665 | result = op2; |
| 1666 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1667 | PyTuple_SetItem(args, 0, result); |
| 1668 | PyTuple_SetItem(args, 1, op2); |
| 1669 | if ((result = PyEval_CallObject(func, args)) == NULL) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1670 | goto Fail; |
| 1671 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1674 | Py_DECREF(args); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1675 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1676 | if (result == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1677 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1678 | "reduce() of empty sequence with no initial value"); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1679 | |
Tim Peters | 15d81ef | 2001-05-04 04:39:21 +0000 | [diff] [blame] | 1680 | Py_DECREF(it); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1681 | return result; |
| 1682 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1683 | Fail: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1684 | Py_XDECREF(args); |
| 1685 | Py_XDECREF(result); |
Tim Peters | 15d81ef | 2001-05-04 04:39:21 +0000 | [diff] [blame] | 1686 | Py_DECREF(it); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1687 | return NULL; |
| 1688 | } |
| 1689 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1690 | PyDoc_STRVAR(reduce_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1691 | "reduce(function, sequence[, initial]) -> value\n\ |
| 1692 | \n\ |
| 1693 | Apply a function of two arguments cumulatively to the items of a sequence,\n\ |
| 1694 | from left to right, so as to reduce the sequence to a single value.\n\ |
| 1695 | For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\ |
| 1696 | ((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\ |
| 1697 | 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] | 1698 | sequence is empty."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1699 | |
| 1700 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1701 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1702 | builtin_reload(PyObject *self, PyObject *v) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1703 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1704 | return PyImport_ReloadModule(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1707 | PyDoc_STRVAR(reload_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1708 | "reload(module) -> module\n\ |
| 1709 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1710 | Reload the module. The module must have been successfully imported before."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1711 | |
| 1712 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1713 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1714 | builtin_repr(PyObject *self, PyObject *v) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1715 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1716 | return PyObject_Repr(v); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1719 | PyDoc_STRVAR(repr_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1720 | "repr(object) -> string\n\ |
| 1721 | \n\ |
| 1722 | Return the canonical string representation of the object.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1723 | For most object types, eval(repr(object)) == object."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1724 | |
| 1725 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1726 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1727 | builtin_round(PyObject *self, PyObject *args) |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1728 | { |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1729 | double x; |
| 1730 | double f; |
| 1731 | int ndigits = 0; |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1732 | int i; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1733 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1734 | if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits)) |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1735 | return NULL; |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1736 | f = 1.0; |
Guido van Rossum | 1e162d3 | 1998-05-09 14:42:25 +0000 | [diff] [blame] | 1737 | i = abs(ndigits); |
| 1738 | while (--i >= 0) |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1739 | f = f*10.0; |
Guido van Rossum | 1e162d3 | 1998-05-09 14:42:25 +0000 | [diff] [blame] | 1740 | if (ndigits < 0) |
| 1741 | x /= f; |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1742 | else |
Guido van Rossum | 1e162d3 | 1998-05-09 14:42:25 +0000 | [diff] [blame] | 1743 | x *= f; |
| 1744 | if (x >= 0.0) |
| 1745 | x = floor(x + 0.5); |
| 1746 | else |
| 1747 | x = ceil(x - 0.5); |
| 1748 | if (ndigits < 0) |
| 1749 | x *= f; |
| 1750 | else |
| 1751 | x /= f; |
| 1752 | return PyFloat_FromDouble(x); |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1755 | PyDoc_STRVAR(round_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1756 | "round(number[, ndigits]) -> floating point number\n\ |
| 1757 | \n\ |
| 1758 | Round a number to a given precision in decimal digits (default 0 digits).\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1759 | This always returns a floating point number. Precision may be negative."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1760 | |
| 1761 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1762 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1763 | builtin_vars(PyObject *self, PyObject *args) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1764 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1765 | PyObject *v = NULL; |
| 1766 | PyObject *d; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1767 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1768 | if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1769 | return NULL; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1770 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1771 | d = PyEval_GetLocals(); |
Guido van Rossum | 53bb7ff | 1995-07-26 16:26:31 +0000 | [diff] [blame] | 1772 | if (d == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1773 | if (!PyErr_Occurred()) |
| 1774 | PyErr_SetString(PyExc_SystemError, |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 1775 | "vars(): no locals!?"); |
Guido van Rossum | 53bb7ff | 1995-07-26 16:26:31 +0000 | [diff] [blame] | 1776 | } |
| 1777 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1778 | Py_INCREF(d); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1779 | } |
| 1780 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1781 | d = PyObject_GetAttrString(v, "__dict__"); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1782 | if (d == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1783 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1784 | "vars() argument must have __dict__ attribute"); |
| 1785 | return NULL; |
| 1786 | } |
| 1787 | } |
| 1788 | return d; |
| 1789 | } |
| 1790 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1791 | PyDoc_STRVAR(vars_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1792 | "vars([object]) -> dictionary\n\ |
| 1793 | \n\ |
| 1794 | Without arguments, equivalent to locals().\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1795 | With an argument, equivalent to object.__dict__."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1796 | |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 1797 | |
| 1798 | static PyObject* |
| 1799 | builtin_sum(PyObject *self, PyObject *args) |
| 1800 | { |
| 1801 | PyObject *seq; |
| 1802 | PyObject *result = NULL; |
| 1803 | PyObject *temp, *item, *iter; |
| 1804 | |
Raymond Hettinger | 5cf6394 | 2003-10-25 06:41:37 +0000 | [diff] [blame] | 1805 | if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result)) |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 1806 | return NULL; |
| 1807 | |
| 1808 | iter = PyObject_GetIter(seq); |
| 1809 | if (iter == NULL) |
| 1810 | return NULL; |
| 1811 | |
| 1812 | if (result == NULL) { |
| 1813 | result = PyInt_FromLong(0); |
| 1814 | if (result == NULL) { |
| 1815 | Py_DECREF(iter); |
| 1816 | return NULL; |
| 1817 | } |
| 1818 | } else { |
| 1819 | /* reject string values for 'start' parameter */ |
| 1820 | if (PyObject_TypeCheck(result, &PyBaseString_Type)) { |
| 1821 | PyErr_SetString(PyExc_TypeError, |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 1822 | "sum() can't sum strings [use ''.join(seq) instead]"); |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 1823 | Py_DECREF(iter); |
| 1824 | return NULL; |
| 1825 | } |
Alex Martelli | 41c9f88 | 2003-04-22 09:24:48 +0000 | [diff] [blame] | 1826 | Py_INCREF(result); |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 1827 | } |
| 1828 | |
| 1829 | for(;;) { |
| 1830 | item = PyIter_Next(iter); |
| 1831 | if (item == NULL) { |
| 1832 | /* error, or end-of-sequence */ |
| 1833 | if (PyErr_Occurred()) { |
| 1834 | Py_DECREF(result); |
| 1835 | result = NULL; |
| 1836 | } |
| 1837 | break; |
| 1838 | } |
Alex Martelli | a253e18 | 2003-10-25 23:24:14 +0000 | [diff] [blame] | 1839 | temp = PyNumber_Add(result, item); |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 1840 | Py_DECREF(result); |
| 1841 | Py_DECREF(item); |
| 1842 | result = temp; |
| 1843 | if (result == NULL) |
| 1844 | break; |
| 1845 | } |
| 1846 | Py_DECREF(iter); |
| 1847 | return result; |
| 1848 | } |
| 1849 | |
| 1850 | PyDoc_STRVAR(sum_doc, |
| 1851 | "sum(sequence, start=0) -> value\n\ |
| 1852 | \n\ |
| 1853 | Returns the sum of a sequence of numbers (NOT strings) plus the value\n\ |
| 1854 | of parameter 'start'. When the sequence is empty, returns start."); |
| 1855 | |
| 1856 | |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1857 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1858 | builtin_isinstance(PyObject *self, PyObject *args) |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1859 | { |
| 1860 | PyObject *inst; |
| 1861 | PyObject *cls; |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 1862 | int retval; |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1863 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1864 | if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls)) |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1865 | return NULL; |
Guido van Rossum | f5dd914 | 1997-12-02 19:11:45 +0000 | [diff] [blame] | 1866 | |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 1867 | retval = PyObject_IsInstance(inst, cls); |
| 1868 | if (retval < 0) |
Guido van Rossum | ad99177 | 2001-01-12 16:03:05 +0000 | [diff] [blame] | 1869 | return NULL; |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1870 | return PyBool_FromLong(retval); |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1873 | PyDoc_STRVAR(isinstance_doc, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1874 | "isinstance(object, class-or-type-or-tuple) -> bool\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1875 | \n\ |
| 1876 | 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] | 1877 | With a type as second argument, return whether that is the object's type.\n\ |
| 1878 | 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] | 1879 | isinstance(x, A) or isinstance(x, B) or ... (etc.)."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1880 | |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1881 | |
| 1882 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1883 | builtin_issubclass(PyObject *self, PyObject *args) |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1884 | { |
| 1885 | PyObject *derived; |
| 1886 | PyObject *cls; |
| 1887 | int retval; |
| 1888 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1889 | if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls)) |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1890 | return NULL; |
Guido van Rossum | 668213d | 1999-06-16 17:28:37 +0000 | [diff] [blame] | 1891 | |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 1892 | retval = PyObject_IsSubclass(derived, cls); |
| 1893 | if (retval < 0) |
| 1894 | return NULL; |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1895 | return PyBool_FromLong(retval); |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1898 | PyDoc_STRVAR(issubclass_doc, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1899 | "issubclass(C, B) -> bool\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1900 | \n\ |
Walter Dörwald | d9a6ad3 | 2002-12-12 16:41:44 +0000 | [diff] [blame] | 1901 | Return whether class C is a subclass (i.e., a derived class) of class B.\n\ |
| 1902 | When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\ |
| 1903 | 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] | 1904 | |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1905 | |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1906 | static PyObject* |
| 1907 | builtin_zip(PyObject *self, PyObject *args) |
| 1908 | { |
| 1909 | PyObject *ret; |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1910 | const int itemsize = PySequence_Length(args); |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 1911 | int i; |
| 1912 | PyObject *itlist; /* tuple of iterators */ |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1913 | int len; /* guess at result length */ |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1914 | |
Raymond Hettinger | eaef615 | 2003-08-02 07:42:57 +0000 | [diff] [blame] | 1915 | if (itemsize == 0) |
| 1916 | return PyList_New(0); |
| 1917 | |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1918 | /* args must be a tuple */ |
| 1919 | assert(PyTuple_Check(args)); |
| 1920 | |
Tim Peters | 39a86c2 | 2002-05-12 07:19:38 +0000 | [diff] [blame] | 1921 | /* Guess at result length: the shortest of the input lengths. |
| 1922 | If some argument refuses to say, we refuse to guess too, lest |
| 1923 | an argument like xrange(sys.maxint) lead us astray.*/ |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1924 | len = -1; /* unknown */ |
| 1925 | for (i = 0; i < itemsize; ++i) { |
| 1926 | PyObject *item = PyTuple_GET_ITEM(args, i); |
| 1927 | int thislen = PySequence_Length(item); |
Tim Peters | 39a86c2 | 2002-05-12 07:19:38 +0000 | [diff] [blame] | 1928 | if (thislen < 0) { |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1929 | PyErr_Clear(); |
Tim Peters | 39a86c2 | 2002-05-12 07:19:38 +0000 | [diff] [blame] | 1930 | len = -1; |
| 1931 | break; |
| 1932 | } |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1933 | else if (len < 0 || thislen < len) |
| 1934 | len = thislen; |
| 1935 | } |
| 1936 | |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 1937 | /* allocate result list */ |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1938 | if (len < 0) |
| 1939 | len = 10; /* arbitrary */ |
| 1940 | if ((ret = PyList_New(len)) == NULL) |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1941 | return NULL; |
| 1942 | |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 1943 | /* obtain iterators */ |
| 1944 | itlist = PyTuple_New(itemsize); |
| 1945 | if (itlist == NULL) |
| 1946 | goto Fail_ret; |
| 1947 | for (i = 0; i < itemsize; ++i) { |
| 1948 | PyObject *item = PyTuple_GET_ITEM(args, i); |
| 1949 | PyObject *it = PyObject_GetIter(item); |
| 1950 | if (it == NULL) { |
| 1951 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1952 | PyErr_Format(PyExc_TypeError, |
| 1953 | "zip argument #%d must support iteration", |
| 1954 | i+1); |
| 1955 | goto Fail_ret_itlist; |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1956 | } |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 1957 | PyTuple_SET_ITEM(itlist, i, it); |
| 1958 | } |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1959 | |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 1960 | /* build result into ret list */ |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1961 | for (i = 0; ; ++i) { |
| 1962 | int j; |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 1963 | PyObject *next = PyTuple_New(itemsize); |
| 1964 | if (!next) |
| 1965 | goto Fail_ret_itlist; |
| 1966 | |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1967 | for (j = 0; j < itemsize; j++) { |
| 1968 | PyObject *it = PyTuple_GET_ITEM(itlist, j); |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 1969 | PyObject *item = PyIter_Next(it); |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1970 | if (!item) { |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 1971 | if (PyErr_Occurred()) { |
| 1972 | Py_DECREF(ret); |
| 1973 | ret = NULL; |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1974 | } |
| 1975 | Py_DECREF(next); |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 1976 | Py_DECREF(itlist); |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1977 | goto Done; |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1978 | } |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1979 | PyTuple_SET_ITEM(next, j, item); |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1980 | } |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 1981 | |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1982 | if (i < len) |
| 1983 | PyList_SET_ITEM(ret, i, next); |
| 1984 | else { |
| 1985 | int status = PyList_Append(ret, next); |
| 1986 | Py_DECREF(next); |
| 1987 | ++len; |
| 1988 | if (status < 0) |
| 1989 | goto Fail_ret_itlist; |
| 1990 | } |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1991 | } |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 1992 | |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1993 | Done: |
| 1994 | if (ret != NULL && i < len) { |
| 1995 | /* The list is too big. */ |
| 1996 | if (PyList_SetSlice(ret, i, len, NULL) < 0) |
| 1997 | return NULL; |
| 1998 | } |
| 1999 | return ret; |
| 2000 | |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 2001 | Fail_ret_itlist: |
| 2002 | Py_DECREF(itlist); |
| 2003 | Fail_ret: |
| 2004 | Py_DECREF(ret); |
| 2005 | return NULL; |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2009 | PyDoc_STRVAR(zip_doc, |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2010 | "zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\ |
| 2011 | \n\ |
| 2012 | Return a list of tuples, where each tuple contains the i-th element\n\ |
| 2013 | 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] | 2014 | in length to the length of the shortest argument sequence."); |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2015 | |
| 2016 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2017 | static PyMethodDef builtin_methods[] = { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2018 | {"__import__", builtin___import__, METH_VARARGS, import_doc}, |
| 2019 | {"abs", builtin_abs, METH_O, abs_doc}, |
| 2020 | {"apply", builtin_apply, METH_VARARGS, apply_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2021 | {"callable", builtin_callable, METH_O, callable_doc}, |
| 2022 | {"chr", builtin_chr, METH_VARARGS, chr_doc}, |
| 2023 | {"cmp", builtin_cmp, METH_VARARGS, cmp_doc}, |
| 2024 | {"coerce", builtin_coerce, METH_VARARGS, coerce_doc}, |
| 2025 | {"compile", builtin_compile, METH_VARARGS, compile_doc}, |
| 2026 | {"delattr", builtin_delattr, METH_VARARGS, delattr_doc}, |
| 2027 | {"dir", builtin_dir, METH_VARARGS, dir_doc}, |
| 2028 | {"divmod", builtin_divmod, METH_VARARGS, divmod_doc}, |
| 2029 | {"eval", builtin_eval, METH_VARARGS, eval_doc}, |
| 2030 | {"execfile", builtin_execfile, METH_VARARGS, execfile_doc}, |
| 2031 | {"filter", builtin_filter, METH_VARARGS, filter_doc}, |
| 2032 | {"getattr", builtin_getattr, METH_VARARGS, getattr_doc}, |
| 2033 | {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc}, |
| 2034 | {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc}, |
| 2035 | {"hash", builtin_hash, METH_O, hash_doc}, |
| 2036 | {"hex", builtin_hex, METH_O, hex_doc}, |
| 2037 | {"id", builtin_id, METH_O, id_doc}, |
| 2038 | {"input", builtin_input, METH_VARARGS, input_doc}, |
| 2039 | {"intern", builtin_intern, METH_VARARGS, intern_doc}, |
| 2040 | {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc}, |
| 2041 | {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc}, |
| 2042 | {"iter", builtin_iter, METH_VARARGS, iter_doc}, |
| 2043 | {"len", builtin_len, METH_O, len_doc}, |
| 2044 | {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc}, |
| 2045 | {"map", builtin_map, METH_VARARGS, map_doc}, |
| 2046 | {"max", builtin_max, METH_VARARGS, max_doc}, |
| 2047 | {"min", builtin_min, METH_VARARGS, min_doc}, |
| 2048 | {"oct", builtin_oct, METH_O, oct_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2049 | {"ord", builtin_ord, METH_O, ord_doc}, |
| 2050 | {"pow", builtin_pow, METH_VARARGS, pow_doc}, |
| 2051 | {"range", builtin_range, METH_VARARGS, range_doc}, |
| 2052 | {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc}, |
| 2053 | {"reduce", builtin_reduce, METH_VARARGS, reduce_doc}, |
| 2054 | {"reload", builtin_reload, METH_O, reload_doc}, |
| 2055 | {"repr", builtin_repr, METH_O, repr_doc}, |
| 2056 | {"round", builtin_round, METH_VARARGS, round_doc}, |
| 2057 | {"setattr", builtin_setattr, METH_VARARGS, setattr_doc}, |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 2058 | {"sum", builtin_sum, METH_VARARGS, sum_doc}, |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2059 | #ifdef Py_USING_UNICODE |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2060 | {"unichr", builtin_unichr, METH_VARARGS, unichr_doc}, |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2061 | #endif |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2062 | {"vars", builtin_vars, METH_VARARGS, vars_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2063 | {"zip", builtin_zip, METH_VARARGS, zip_doc}, |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 2064 | {NULL, NULL}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2065 | }; |
| 2066 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2067 | PyDoc_STRVAR(builtin_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2068 | "Built-in functions, exceptions, and other objects.\n\ |
| 2069 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2070 | Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2071 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2072 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2073 | _PyBuiltin_Init(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2074 | { |
Fred Drake | 5550de3 | 2000-06-20 04:54:19 +0000 | [diff] [blame] | 2075 | PyObject *mod, *dict, *debug; |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2076 | mod = Py_InitModule4("__builtin__", builtin_methods, |
| 2077 | builtin_doc, (PyObject *)NULL, |
| 2078 | PYTHON_API_VERSION); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2079 | if (mod == NULL) |
| 2080 | return NULL; |
| 2081 | dict = PyModule_GetDict(mod); |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2082 | |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 2083 | #ifdef Py_TRACE_REFS |
| 2084 | /* __builtin__ exposes a number of statically allocated objects |
| 2085 | * that, before this code was added in 2.3, never showed up in |
| 2086 | * the list of "all objects" maintained by Py_TRACE_REFS. As a |
| 2087 | * result, programs leaking references to None and False (etc) |
| 2088 | * couldn't be diagnosed by examining sys.getobjects(0). |
| 2089 | */ |
| 2090 | #define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0) |
| 2091 | #else |
| 2092 | #define ADD_TO_ALL(OBJECT) (void)0 |
| 2093 | #endif |
| 2094 | |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2095 | #define SETBUILTIN(NAME, OBJECT) \ |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 2096 | if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \ |
| 2097 | return NULL; \ |
| 2098 | ADD_TO_ALL(OBJECT) |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2099 | |
| 2100 | SETBUILTIN("None", Py_None); |
| 2101 | SETBUILTIN("Ellipsis", Py_Ellipsis); |
| 2102 | SETBUILTIN("NotImplemented", Py_NotImplemented); |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 2103 | SETBUILTIN("False", Py_False); |
| 2104 | SETBUILTIN("True", Py_True); |
Neal Norwitz | 32a7e7f | 2002-05-31 19:58:02 +0000 | [diff] [blame] | 2105 | SETBUILTIN("basestring", &PyBaseString_Type); |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 2106 | SETBUILTIN("bool", &PyBool_Type); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 2107 | SETBUILTIN("buffer", &PyBuffer_Type); |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2108 | SETBUILTIN("classmethod", &PyClassMethod_Type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2109 | #ifndef WITHOUT_COMPLEX |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2110 | SETBUILTIN("complex", &PyComplex_Type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2111 | #endif |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 2112 | SETBUILTIN("dict", &PyDict_Type); |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 2113 | SETBUILTIN("enumerate", &PyEnum_Type); |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2114 | SETBUILTIN("float", &PyFloat_Type); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2115 | SETBUILTIN("frozenset", &PyFrozenSet_Type); |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2116 | SETBUILTIN("property", &PyProperty_Type); |
| 2117 | SETBUILTIN("int", &PyInt_Type); |
| 2118 | SETBUILTIN("list", &PyList_Type); |
| 2119 | SETBUILTIN("long", &PyLong_Type); |
| 2120 | SETBUILTIN("object", &PyBaseObject_Type); |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 2121 | SETBUILTIN("reversed", &PyReversed_Type); |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 2122 | SETBUILTIN("set", &PySet_Type); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 2123 | SETBUILTIN("slice", &PySlice_Type); |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2124 | SETBUILTIN("staticmethod", &PyStaticMethod_Type); |
| 2125 | SETBUILTIN("str", &PyString_Type); |
| 2126 | SETBUILTIN("super", &PySuper_Type); |
| 2127 | SETBUILTIN("tuple", &PyTuple_Type); |
| 2128 | SETBUILTIN("type", &PyType_Type); |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 2129 | SETBUILTIN("xrange", &PyRange_Type); |
Tim Peters | 742dfd6 | 2001-09-13 21:49:44 +0000 | [diff] [blame] | 2130 | |
| 2131 | /* Note that open() is just an alias of file(). */ |
| 2132 | SETBUILTIN("open", &PyFile_Type); |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2133 | SETBUILTIN("file", &PyFile_Type); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2134 | #ifdef Py_USING_UNICODE |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2135 | SETBUILTIN("unicode", &PyUnicode_Type); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2136 | #endif |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 2137 | debug = PyBool_FromLong(Py_OptimizeFlag == 0); |
Fred Drake | 5550de3 | 2000-06-20 04:54:19 +0000 | [diff] [blame] | 2138 | if (PyDict_SetItemString(dict, "__debug__", debug) < 0) { |
| 2139 | Py_XDECREF(debug); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2140 | return NULL; |
Fred Drake | 5550de3 | 2000-06-20 04:54:19 +0000 | [diff] [blame] | 2141 | } |
| 2142 | Py_XDECREF(debug); |
Barry Warsaw | 757af0e | 1997-08-29 22:13:51 +0000 | [diff] [blame] | 2143 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2144 | return mod; |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 2145 | #undef ADD_TO_ALL |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2146 | #undef SETBUILTIN |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 2149 | /* Helper for filter(): filter a tuple through a function */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2150 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2151 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2152 | filtertuple(PyObject *func, PyObject *tuple) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2153 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2154 | PyObject *result; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2155 | register int i, j; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2156 | int len = PyTuple_Size(tuple); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2157 | |
Guido van Rossum | b7b4562 | 1995-08-04 04:07:45 +0000 | [diff] [blame] | 2158 | if (len == 0) { |
Walter Dörwald | c3da83f | 2003-02-04 20:24:45 +0000 | [diff] [blame] | 2159 | if (PyTuple_CheckExact(tuple)) |
| 2160 | Py_INCREF(tuple); |
| 2161 | else |
| 2162 | tuple = PyTuple_New(0); |
Guido van Rossum | b7b4562 | 1995-08-04 04:07:45 +0000 | [diff] [blame] | 2163 | return tuple; |
| 2164 | } |
| 2165 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2166 | if ((result = PyTuple_New(len)) == NULL) |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 2167 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2168 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2169 | for (i = j = 0; i < len; ++i) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2170 | PyObject *item, *good; |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2171 | int ok; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2172 | |
Walter Dörwald | 8dd1932 | 2003-02-10 17:36:40 +0000 | [diff] [blame] | 2173 | if (tuple->ob_type->tp_as_sequence && |
| 2174 | tuple->ob_type->tp_as_sequence->sq_item) { |
| 2175 | item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i); |
Walter Dörwald | c58a3a1 | 2003-08-18 18:28:45 +0000 | [diff] [blame] | 2176 | if (item == NULL) |
| 2177 | goto Fail_1; |
Walter Dörwald | 8dd1932 | 2003-02-10 17:36:40 +0000 | [diff] [blame] | 2178 | } else { |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 2179 | PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple"); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2180 | goto Fail_1; |
Walter Dörwald | 8dd1932 | 2003-02-10 17:36:40 +0000 | [diff] [blame] | 2181 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2182 | if (func == Py_None) { |
| 2183 | Py_INCREF(item); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2184 | good = item; |
| 2185 | } |
| 2186 | else { |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 2187 | PyObject *arg = PyTuple_Pack(1, item); |
Walter Dörwald | c58a3a1 | 2003-08-18 18:28:45 +0000 | [diff] [blame] | 2188 | if (arg == NULL) { |
| 2189 | Py_DECREF(item); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2190 | goto Fail_1; |
Walter Dörwald | c58a3a1 | 2003-08-18 18:28:45 +0000 | [diff] [blame] | 2191 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2192 | good = PyEval_CallObject(func, arg); |
| 2193 | Py_DECREF(arg); |
Walter Dörwald | c58a3a1 | 2003-08-18 18:28:45 +0000 | [diff] [blame] | 2194 | if (good == NULL) { |
| 2195 | Py_DECREF(item); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2196 | goto Fail_1; |
Walter Dörwald | c58a3a1 | 2003-08-18 18:28:45 +0000 | [diff] [blame] | 2197 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2198 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2199 | ok = PyObject_IsTrue(good); |
| 2200 | Py_DECREF(good); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2201 | if (ok) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2202 | if (PyTuple_SetItem(result, j++, item) < 0) |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2203 | goto Fail_1; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2204 | } |
Walter Dörwald | c58a3a1 | 2003-08-18 18:28:45 +0000 | [diff] [blame] | 2205 | else |
| 2206 | Py_DECREF(item); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2207 | } |
| 2208 | |
Tim Peters | 4324aa3 | 2001-05-28 22:30:08 +0000 | [diff] [blame] | 2209 | if (_PyTuple_Resize(&result, j) < 0) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2210 | return NULL; |
| 2211 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2212 | return result; |
| 2213 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2214 | Fail_1: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2215 | Py_DECREF(result); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2216 | return NULL; |
| 2217 | } |
| 2218 | |
| 2219 | |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 2220 | /* Helper for filter(): filter a string through a function */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2221 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2222 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2223 | filterstring(PyObject *func, PyObject *strobj) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2224 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2225 | PyObject *result; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2226 | register int i, j; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2227 | int len = PyString_Size(strobj); |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2228 | int outlen = len; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2229 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2230 | if (func == Py_None) { |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 2231 | /* If it's a real string we can return the original, |
| 2232 | * as no character is ever false and __getitem__ |
| 2233 | * does return this character. If it's a subclass |
| 2234 | * we must go through the __getitem__ loop */ |
| 2235 | if (PyString_CheckExact(strobj)) { |
Walter Dörwald | c3da83f | 2003-02-04 20:24:45 +0000 | [diff] [blame] | 2236 | Py_INCREF(strobj); |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 2237 | return strobj; |
| 2238 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2239 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2240 | if ((result = PyString_FromStringAndSize(NULL, len)) == NULL) |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 2241 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2242 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2243 | for (i = j = 0; i < len; ++i) { |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 2244 | PyObject *item; |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2245 | int ok; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2246 | |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2247 | item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i); |
| 2248 | if (item == NULL) |
| 2249 | goto Fail_1; |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 2250 | if (func==Py_None) { |
| 2251 | ok = 1; |
| 2252 | } else { |
| 2253 | PyObject *arg, *good; |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 2254 | arg = PyTuple_Pack(1, item); |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 2255 | if (arg == NULL) { |
| 2256 | Py_DECREF(item); |
| 2257 | goto Fail_1; |
| 2258 | } |
| 2259 | good = PyEval_CallObject(func, arg); |
| 2260 | Py_DECREF(arg); |
| 2261 | if (good == NULL) { |
| 2262 | Py_DECREF(item); |
| 2263 | goto Fail_1; |
| 2264 | } |
| 2265 | ok = PyObject_IsTrue(good); |
| 2266 | Py_DECREF(good); |
Tim Peters | 388ed08 | 2001-04-07 20:34:48 +0000 | [diff] [blame] | 2267 | } |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2268 | if (ok) { |
| 2269 | int reslen; |
| 2270 | if (!PyString_Check(item)) { |
| 2271 | PyErr_SetString(PyExc_TypeError, "can't filter str to str:" |
| 2272 | " __getitem__ returned different type"); |
| 2273 | Py_DECREF(item); |
| 2274 | goto Fail_1; |
| 2275 | } |
| 2276 | reslen = PyString_GET_SIZE(item); |
| 2277 | if (reslen == 1) { |
| 2278 | PyString_AS_STRING(result)[j++] = |
| 2279 | PyString_AS_STRING(item)[0]; |
| 2280 | } else { |
| 2281 | /* do we need more space? */ |
| 2282 | int need = j + reslen + len-i-1; |
| 2283 | if (need > outlen) { |
| 2284 | /* overallocate, to avoid reallocations */ |
| 2285 | if (need<2*outlen) |
| 2286 | need = 2*outlen; |
| 2287 | if (_PyString_Resize(&result, need)) { |
| 2288 | Py_DECREF(item); |
| 2289 | return NULL; |
| 2290 | } |
| 2291 | outlen = need; |
| 2292 | } |
| 2293 | memcpy( |
| 2294 | PyString_AS_STRING(result) + j, |
| 2295 | PyString_AS_STRING(item), |
| 2296 | reslen |
| 2297 | ); |
| 2298 | j += reslen; |
| 2299 | } |
| 2300 | } |
Tim Peters | 388ed08 | 2001-04-07 20:34:48 +0000 | [diff] [blame] | 2301 | Py_DECREF(item); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2304 | if (j < outlen) |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2305 | _PyString_Resize(&result, j); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2306 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2307 | return result; |
| 2308 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2309 | Fail_1: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2310 | Py_DECREF(result); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2311 | return NULL; |
| 2312 | } |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 2313 | |
| 2314 | #ifdef Py_USING_UNICODE |
| 2315 | /* Helper for filter(): filter a Unicode object through a function */ |
| 2316 | |
| 2317 | static PyObject * |
| 2318 | filterunicode(PyObject *func, PyObject *strobj) |
| 2319 | { |
| 2320 | PyObject *result; |
| 2321 | register int i, j; |
| 2322 | int len = PyUnicode_GetSize(strobj); |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2323 | int outlen = len; |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 2324 | |
| 2325 | if (func == Py_None) { |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 2326 | /* If it's a real string we can return the original, |
| 2327 | * as no character is ever false and __getitem__ |
| 2328 | * does return this character. If it's a subclass |
| 2329 | * we must go through the __getitem__ loop */ |
| 2330 | if (PyUnicode_CheckExact(strobj)) { |
Walter Dörwald | c3da83f | 2003-02-04 20:24:45 +0000 | [diff] [blame] | 2331 | Py_INCREF(strobj); |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 2332 | return strobj; |
| 2333 | } |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 2334 | } |
| 2335 | if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL) |
| 2336 | return NULL; |
| 2337 | |
| 2338 | for (i = j = 0; i < len; ++i) { |
| 2339 | PyObject *item, *arg, *good; |
| 2340 | int ok; |
| 2341 | |
| 2342 | item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i); |
| 2343 | if (item == NULL) |
| 2344 | goto Fail_1; |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 2345 | if (func == Py_None) { |
| 2346 | ok = 1; |
| 2347 | } else { |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 2348 | arg = PyTuple_Pack(1, item); |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 2349 | if (arg == NULL) { |
| 2350 | Py_DECREF(item); |
| 2351 | goto Fail_1; |
| 2352 | } |
| 2353 | good = PyEval_CallObject(func, arg); |
| 2354 | Py_DECREF(arg); |
| 2355 | if (good == NULL) { |
| 2356 | Py_DECREF(item); |
| 2357 | goto Fail_1; |
| 2358 | } |
| 2359 | ok = PyObject_IsTrue(good); |
| 2360 | Py_DECREF(good); |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 2361 | } |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2362 | if (ok) { |
| 2363 | int reslen; |
| 2364 | if (!PyUnicode_Check(item)) { |
Jeremy Hylton | 1aad9c7 | 2003-09-16 03:10:59 +0000 | [diff] [blame] | 2365 | PyErr_SetString(PyExc_TypeError, |
| 2366 | "can't filter unicode to unicode:" |
| 2367 | " __getitem__ returned different type"); |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2368 | Py_DECREF(item); |
| 2369 | goto Fail_1; |
| 2370 | } |
| 2371 | reslen = PyUnicode_GET_SIZE(item); |
Jeremy Hylton | 1aad9c7 | 2003-09-16 03:10:59 +0000 | [diff] [blame] | 2372 | if (reslen == 1) |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2373 | PyUnicode_AS_UNICODE(result)[j++] = |
| 2374 | PyUnicode_AS_UNICODE(item)[0]; |
Jeremy Hylton | 1aad9c7 | 2003-09-16 03:10:59 +0000 | [diff] [blame] | 2375 | else { |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2376 | /* do we need more space? */ |
Jeremy Hylton | 1aad9c7 | 2003-09-16 03:10:59 +0000 | [diff] [blame] | 2377 | int need = j + reslen + len - i - 1; |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2378 | if (need > outlen) { |
Jeremy Hylton | 1aad9c7 | 2003-09-16 03:10:59 +0000 | [diff] [blame] | 2379 | /* overallocate, |
| 2380 | to avoid reallocations */ |
| 2381 | if (need < 2 * outlen) |
| 2382 | need = 2 * outlen; |
Jeremy Hylton | 364f6be | 2003-09-16 03:17:16 +0000 | [diff] [blame] | 2383 | if (PyUnicode_Resize( |
| 2384 | &result, need) < 0) { |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2385 | Py_DECREF(item); |
Walter Dörwald | 531e000 | 2003-02-04 16:57:49 +0000 | [diff] [blame] | 2386 | goto Fail_1; |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2387 | } |
| 2388 | outlen = need; |
| 2389 | } |
Jeremy Hylton | 1aad9c7 | 2003-09-16 03:10:59 +0000 | [diff] [blame] | 2390 | memcpy(PyUnicode_AS_UNICODE(result) + j, |
| 2391 | PyUnicode_AS_UNICODE(item), |
| 2392 | reslen*sizeof(Py_UNICODE)); |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2393 | j += reslen; |
| 2394 | } |
| 2395 | } |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 2396 | Py_DECREF(item); |
| 2397 | } |
| 2398 | |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2399 | if (j < outlen) |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 2400 | PyUnicode_Resize(&result, j); |
| 2401 | |
| 2402 | return result; |
| 2403 | |
| 2404 | Fail_1: |
| 2405 | Py_DECREF(result); |
| 2406 | return NULL; |
| 2407 | } |
| 2408 | #endif |