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