Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 32 | /* Built-in functions */ |
| 33 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 35 | |
| 36 | #include "node.h" |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 37 | #include "compile.h" |
| 38 | #include "eval.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 39 | |
Guido van Rossum | fe4b6ee | 1996-08-08 18:49:41 +0000 | [diff] [blame] | 40 | #include "mymath.h" |
| 41 | |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 42 | #include <ctype.h> |
| 43 | |
Guido van Rossum | 1a2c5cb | 1996-12-10 15:37:36 +0000 | [diff] [blame] | 44 | #ifdef HAVE_UNISTD_H |
| 45 | #include <unistd.h> |
| 46 | #endif |
| 47 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 48 | /* Forward */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 49 | static PyObject *filterstring Py_PROTO((PyObject *, PyObject *)); |
| 50 | static PyObject *filtertuple Py_PROTO((PyObject *, PyObject *)); |
| 51 | static PyObject *int_from_string Py_PROTO((PyObject *)); |
| 52 | static PyObject *long_from_string Py_PROTO((PyObject *)); |
| 53 | static PyObject *float_from_string Py_PROTO((PyObject *)); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 55 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 56 | builtin___import__(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 57 | PyObject *self; |
| 58 | PyObject *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 59 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 60 | char *name; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 61 | PyObject *globals = NULL; |
| 62 | PyObject *locals = NULL; |
| 63 | PyObject *fromlist = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 65 | if (!PyArg_ParseTuple(args, "s|OOO:__import__", |
Guido van Rossum | 24c1374 | 1995-02-14 09:42:43 +0000 | [diff] [blame] | 66 | &name, &globals, &locals, &fromlist)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 67 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 68 | return PyImport_ImportModuleEx(name, globals, locals, fromlist); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 72 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 73 | builtin_abs(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 74 | PyObject *self; |
| 75 | PyObject *args; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 76 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 77 | PyObject *v; |
| 78 | PyNumberMethods *nm; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 80 | if (!PyArg_ParseTuple(args, "O:abs", &v)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 81 | return NULL; |
| 82 | if ((nm = v->ob_type->tp_as_number) == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 83 | PyErr_SetString(PyExc_TypeError, |
| 84 | "abs() requires numeric argument"); |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 85 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 86 | } |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 87 | return (*nm->nb_absolute)(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 90 | static PyObject * |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 91 | builtin_apply(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 92 | PyObject *self; |
| 93 | PyObject *args; |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 94 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 95 | PyObject *func, *alist = NULL, *kwdict = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 97 | if (!PyArg_ParseTuple(args, "O|OO:apply", &func, &alist, &kwdict)) |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 98 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 99 | if (alist != NULL && !PyTuple_Check(alist)) { |
| 100 | PyErr_SetString(PyExc_TypeError, |
| 101 | "apply() 2nd argument must be tuple"); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 102 | return NULL; |
| 103 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 104 | if (kwdict != NULL && !PyDict_Check(kwdict)) { |
| 105 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 106 | "apply() 3rd argument must be dictionary"); |
| 107 | return NULL; |
| 108 | } |
| 109 | return PyEval_CallObjectWithKeywords(func, alist, kwdict); |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 112 | static PyObject * |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 113 | builtin_callable(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 114 | PyObject *self; |
| 115 | PyObject *args; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 116 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 117 | PyObject *v; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 118 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 119 | if (!PyArg_ParseTuple(args, "O:callable", &v)) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 120 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 121 | return PyInt_FromLong((long)PyCallable_Check(v)); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 124 | static PyObject * |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 125 | builtin_filter(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 126 | PyObject *self; |
| 127 | PyObject *args; |
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 | PyObject *func, *seq, *result; |
| 130 | PySequenceMethods *sqf; |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 131 | int len; |
| 132 | register int i, j; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 133 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 134 | if (!PyArg_ParseTuple(args, "OO:filter", &func, &seq)) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 135 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 136 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 137 | if (PyString_Check(seq)) { |
| 138 | PyObject *r = filterstring(func, seq); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 139 | return r; |
| 140 | } |
| 141 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 142 | if (PyTuple_Check(seq)) { |
| 143 | PyObject *r = filtertuple(func, seq); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 144 | return r; |
| 145 | } |
| 146 | |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 147 | if ((sqf = seq->ob_type->tp_as_sequence) == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 148 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 149 | "argument 2 to filter() must be a sequence type"); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 150 | goto Fail_2; |
| 151 | } |
| 152 | |
| 153 | if ((len = (*sqf->sq_length)(seq)) < 0) |
| 154 | goto Fail_2; |
| 155 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 156 | if (PyList_Check(seq) && seq->ob_refcnt == 1) { |
| 157 | Py_INCREF(seq); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 158 | result = seq; |
| 159 | } |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 160 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 161 | if ((result = PyList_New(len)) == NULL) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 162 | goto Fail_2; |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 163 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 164 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 165 | for (i = j = 0; ; ++i) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 166 | PyObject *item, *good; |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 167 | int ok; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 168 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 169 | if ((item = (*sqf->sq_item)(seq, i)) == NULL) { |
| 170 | if (i < len) |
| 171 | goto Fail_1; |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 172 | if (PyErr_ExceptionMatches(PyExc_IndexError)) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 173 | PyErr_Clear(); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 174 | break; |
| 175 | } |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 176 | goto Fail_1; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 177 | } |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 178 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 179 | if (func == Py_None) { |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 180 | good = item; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 181 | Py_INCREF(good); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 182 | } |
| 183 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 184 | PyObject *arg = Py_BuildValue("(O)", item); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 185 | if (arg == NULL) |
| 186 | goto Fail_1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 187 | good = PyEval_CallObject(func, arg); |
| 188 | Py_DECREF(arg); |
Guido van Rossum | 58b6873 | 1995-01-10 17:40:55 +0000 | [diff] [blame] | 189 | if (good == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 190 | Py_DECREF(item); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 191 | goto Fail_1; |
Guido van Rossum | 58b6873 | 1995-01-10 17:40:55 +0000 | [diff] [blame] | 192 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 193 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 194 | ok = PyObject_IsTrue(good); |
| 195 | Py_DECREF(good); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 196 | if (ok) { |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 197 | if (j < len) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 198 | if (PyList_SetItem(result, j++, item) < 0) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 199 | goto Fail_1; |
| 200 | } |
| 201 | else { |
| 202 | j++; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 203 | if (PyList_Append(result, item) < 0) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 204 | goto Fail_1; |
| 205 | } |
Guido van Rossum | 58b6873 | 1995-01-10 17:40:55 +0000 | [diff] [blame] | 206 | } else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 207 | Py_DECREF(item); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 208 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 211 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 212 | if (j < len && PyList_SetSlice(result, j, len, NULL) < 0) |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 213 | goto Fail_1; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 214 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 215 | return result; |
| 216 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 217 | Fail_1: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 218 | Py_DECREF(result); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 219 | Fail_2: |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 220 | return NULL; |
| 221 | } |
| 222 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 223 | static PyObject * |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 224 | builtin_chr(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 225 | PyObject *self; |
| 226 | PyObject *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 227 | { |
| 228 | long x; |
| 229 | char s[1]; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 230 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 231 | if (!PyArg_ParseTuple(args, "l:chr", &x)) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 232 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 233 | if (x < 0 || x >= 256) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 234 | PyErr_SetString(PyExc_ValueError, |
| 235 | "chr() arg not in range(256)"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 236 | return NULL; |
| 237 | } |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 238 | s[0] = (char)x; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 239 | return PyString_FromStringAndSize(s, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 242 | static PyObject * |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 243 | builtin_cmp(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 244 | PyObject *self; |
| 245 | PyObject *args; |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 246 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 247 | PyObject *a, *b; |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 248 | long c; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 249 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 250 | if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b)) |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 251 | return NULL; |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 252 | c = PyObject_Compare(a, b); |
| 253 | if (c && PyErr_Occurred()) |
| 254 | return NULL; |
| 255 | return PyInt_FromLong(c); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 258 | static PyObject * |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 259 | builtin_coerce(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 260 | PyObject *self; |
| 261 | PyObject *args; |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 262 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 263 | PyObject *v, *w; |
| 264 | PyObject *res; |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 265 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 266 | if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w)) |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 267 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 268 | if (PyNumber_Coerce(&v, &w) < 0) |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 269 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 270 | res = Py_BuildValue("(OO)", v, w); |
| 271 | Py_DECREF(v); |
| 272 | Py_DECREF(w); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 273 | return res; |
| 274 | } |
| 275 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 276 | static PyObject * |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 277 | builtin_compile(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 278 | PyObject *self; |
| 279 | PyObject *args; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 280 | { |
| 281 | char *str; |
| 282 | char *filename; |
| 283 | char *startstr; |
| 284 | int start; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 285 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 286 | if (!PyArg_ParseTuple(args, "sss:compile", &str, &filename, &startstr)) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 287 | return NULL; |
| 288 | if (strcmp(startstr, "exec") == 0) |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 289 | start = Py_file_input; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 290 | else if (strcmp(startstr, "eval") == 0) |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 291 | start = Py_eval_input; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 292 | else if (strcmp(startstr, "single") == 0) |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 293 | start = Py_single_input; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 294 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 295 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 296 | "compile() mode must be 'exec' or 'eval' or 'single'"); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 297 | return NULL; |
| 298 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 299 | return Py_CompileString(str, filename, start); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 302 | #ifndef WITHOUT_COMPLEX |
| 303 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 304 | static PyObject * |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 305 | builtin_complex(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 306 | PyObject *self; |
| 307 | PyObject *args; |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 308 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 309 | PyObject *r, *i, *tmp; |
| 310 | PyNumberMethods *nbr, *nbi = NULL; |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 311 | Py_complex cr, ci; |
Guido van Rossum | c6472e9 | 1997-03-31 17:15:43 +0000 | [diff] [blame] | 312 | int own_r = 0; |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 313 | |
| 314 | i = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 315 | if (!PyArg_ParseTuple(args, "O|O:complex", &r, &i)) |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 316 | return NULL; |
| 317 | if ((nbr = r->ob_type->tp_as_number) == NULL || |
Guido van Rossum | c6472e9 | 1997-03-31 17:15:43 +0000 | [diff] [blame] | 318 | nbr->nb_float == NULL || |
| 319 | (i != NULL && |
| 320 | ((nbi = i->ob_type->tp_as_number) == NULL || |
| 321 | nbi->nb_float == NULL))) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 322 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 323 | "complex() argument can't be converted to complex"); |
| 324 | return NULL; |
| 325 | } |
Guido van Rossum | ed0af8f | 1996-12-05 23:18:18 +0000 | [diff] [blame] | 326 | /* XXX Hack to support classes with __complex__ method */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 327 | if (PyInstance_Check(r)) { |
| 328 | static PyObject *complexstr; |
| 329 | PyObject *f; |
Guido van Rossum | ed0af8f | 1996-12-05 23:18:18 +0000 | [diff] [blame] | 330 | if (complexstr == NULL) { |
Guido van Rossum | 8d75161 | 1997-01-18 08:04:16 +0000 | [diff] [blame] | 331 | complexstr = PyString_InternFromString("__complex__"); |
Guido van Rossum | ed0af8f | 1996-12-05 23:18:18 +0000 | [diff] [blame] | 332 | if (complexstr == NULL) |
| 333 | return NULL; |
| 334 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 335 | f = PyObject_GetAttr(r, complexstr); |
Guido van Rossum | ed0af8f | 1996-12-05 23:18:18 +0000 | [diff] [blame] | 336 | if (f == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 337 | PyErr_Clear(); |
Guido van Rossum | ed0af8f | 1996-12-05 23:18:18 +0000 | [diff] [blame] | 338 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 339 | PyObject *args = Py_BuildValue("()"); |
Guido van Rossum | ed0af8f | 1996-12-05 23:18:18 +0000 | [diff] [blame] | 340 | if (args == NULL) |
| 341 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 342 | r = PyEval_CallObject(f, args); |
| 343 | Py_DECREF(args); |
Guido van Rossum | ed0af8f | 1996-12-05 23:18:18 +0000 | [diff] [blame] | 344 | if (r == NULL) |
| 345 | return NULL; |
Guido van Rossum | c6472e9 | 1997-03-31 17:15:43 +0000 | [diff] [blame] | 346 | own_r = 1; |
Guido van Rossum | ed0af8f | 1996-12-05 23:18:18 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 349 | if (PyComplex_Check(r)) { |
| 350 | cr = ((PyComplexObject*)r)->cval; |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 351 | if (own_r) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 352 | Py_DECREF(r); |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 353 | } |
Guido van Rossum | c6472e9 | 1997-03-31 17:15:43 +0000 | [diff] [blame] | 354 | } |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 355 | else { |
Guido van Rossum | fe4b6ee | 1996-08-08 18:49:41 +0000 | [diff] [blame] | 356 | tmp = (*nbr->nb_float)(r); |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 357 | if (own_r) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 358 | Py_DECREF(r); |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 359 | } |
Guido van Rossum | fe4b6ee | 1996-08-08 18:49:41 +0000 | [diff] [blame] | 360 | if (tmp == NULL) |
| 361 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 362 | cr.real = PyFloat_AsDouble(tmp); |
| 363 | Py_DECREF(tmp); |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 364 | cr.imag = 0.; |
| 365 | } |
| 366 | if (i == NULL) { |
| 367 | ci.real = 0.; |
| 368 | ci.imag = 0.; |
| 369 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 370 | else if (PyComplex_Check(i)) |
| 371 | ci = ((PyComplexObject*)i)->cval; |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 372 | else { |
Guido van Rossum | c6472e9 | 1997-03-31 17:15:43 +0000 | [diff] [blame] | 373 | tmp = (*nbi->nb_float)(i); |
Guido van Rossum | fe4b6ee | 1996-08-08 18:49:41 +0000 | [diff] [blame] | 374 | if (tmp == NULL) |
| 375 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 376 | ci.real = PyFloat_AsDouble(tmp); |
| 377 | Py_DECREF(tmp); |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 378 | ci.imag = 0.; |
| 379 | } |
| 380 | cr.real -= ci.imag; |
| 381 | cr.imag += ci.real; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 382 | return PyComplex_FromCComplex(cr); |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | #endif |
| 386 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 387 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 388 | builtin_dir(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 389 | PyObject *self; |
| 390 | PyObject *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 391 | { |
Guido van Rossum | 666b17a | 1997-05-06 16:36:57 +0000 | [diff] [blame] | 392 | static char *attrlist[] = {"__members__", "__methods__", NULL}; |
| 393 | PyObject *v = NULL, *l = NULL, *m = NULL; |
| 394 | PyObject *d, *x; |
| 395 | int i; |
| 396 | char **s; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 397 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 398 | if (!PyArg_ParseTuple(args, "|O:dir", &v)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 399 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 400 | if (v == NULL) { |
Guido van Rossum | 666b17a | 1997-05-06 16:36:57 +0000 | [diff] [blame] | 401 | x = PyEval_GetLocals(); |
| 402 | if (x == NULL) |
| 403 | goto error; |
| 404 | l = PyMapping_Keys(x); |
| 405 | if (l == NULL) |
| 406 | goto error; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 407 | } |
| 408 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 409 | d = PyObject_GetAttrString(v, "__dict__"); |
Guido van Rossum | 666b17a | 1997-05-06 16:36:57 +0000 | [diff] [blame] | 410 | if (d == NULL) |
Guido van Rossum | 795ba58 | 1996-05-23 22:49:07 +0000 | [diff] [blame] | 411 | PyErr_Clear(); |
Guido van Rossum | 666b17a | 1997-05-06 16:36:57 +0000 | [diff] [blame] | 412 | else { |
| 413 | l = PyMapping_Keys(d); |
| 414 | if (l == NULL) |
| 415 | PyErr_Clear(); |
| 416 | Py_DECREF(d); |
| 417 | } |
| 418 | if (l == NULL) { |
| 419 | l = PyList_New(0); |
| 420 | if (l == NULL) |
| 421 | goto error; |
| 422 | } |
| 423 | for (s = attrlist; *s != NULL; s++) { |
| 424 | m = PyObject_GetAttrString(v, *s); |
| 425 | if (m == NULL) { |
| 426 | PyErr_Clear(); |
| 427 | continue; |
| 428 | } |
| 429 | for (i = 0; ; i++) { |
| 430 | x = PySequence_GetItem(m, i); |
| 431 | if (x == NULL) { |
| 432 | PyErr_Clear(); |
| 433 | break; |
| 434 | } |
| 435 | if (PyList_Append(l, x) != 0) { |
| 436 | Py_DECREF(x); |
| 437 | Py_DECREF(m); |
| 438 | goto error; |
| 439 | } |
| 440 | Py_DECREF(x); |
| 441 | } |
| 442 | Py_DECREF(m); |
Guido van Rossum | 795ba58 | 1996-05-23 22:49:07 +0000 | [diff] [blame] | 443 | } |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 444 | } |
Guido van Rossum | 666b17a | 1997-05-06 16:36:57 +0000 | [diff] [blame] | 445 | if (PyList_Sort(l) != 0) |
| 446 | goto error; |
| 447 | return l; |
| 448 | error: |
| 449 | Py_XDECREF(l); |
| 450 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 453 | static PyObject * |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 454 | do_divmod(v, w) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 455 | PyObject *v, *w; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 456 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 457 | PyObject *res; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 458 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 459 | if (PyInstance_Check(v) || PyInstance_Check(w)) |
| 460 | return PyInstance_DoBinOp(v, w, "__divmod__", "__rdivmod__", |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 461 | do_divmod); |
Guido van Rossum | c6bb8f7 | 1991-07-01 18:42:41 +0000 | [diff] [blame] | 462 | if (v->ob_type->tp_as_number == NULL || |
| 463 | w->ob_type->tp_as_number == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 464 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 465 | "divmod() requires numeric or class instance arguments"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 466 | return NULL; |
| 467 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 468 | if (PyNumber_Coerce(&v, &w) != 0) |
Guido van Rossum | c6bb8f7 | 1991-07-01 18:42:41 +0000 | [diff] [blame] | 469 | return NULL; |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 470 | res = (*v->ob_type->tp_as_number->nb_divmod)(v, w); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 471 | Py_DECREF(v); |
| 472 | Py_DECREF(w); |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 473 | return res; |
| 474 | } |
| 475 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 476 | static PyObject * |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 477 | builtin_divmod(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 478 | PyObject *self; |
| 479 | PyObject *args; |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 480 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 481 | PyObject *v, *w; |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 482 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 483 | if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w)) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 484 | return NULL; |
| 485 | return do_divmod(v, w); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 488 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 489 | builtin_eval(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 490 | PyObject *self; |
| 491 | PyObject *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 492 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 493 | PyObject *cmd; |
| 494 | PyObject *globals = Py_None, *locals = Py_None; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 495 | char *str; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 496 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 497 | if (!PyArg_ParseTuple(args, "O|O!O!:eval", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 498 | &cmd, |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 499 | &PyDict_Type, &globals, |
| 500 | &PyDict_Type, &locals)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 501 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 502 | if (globals == Py_None) { |
| 503 | globals = PyEval_GetGlobals(); |
| 504 | if (locals == Py_None) |
| 505 | locals = PyEval_GetLocals(); |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 506 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 507 | else if (locals == Py_None) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 508 | locals = globals; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 509 | if (PyDict_GetItemString(globals, "__builtins__") == NULL) { |
| 510 | if (PyDict_SetItemString(globals, "__builtins__", |
| 511 | PyEval_GetBuiltins()) != 0) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 512 | return NULL; |
| 513 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 514 | if (PyCode_Check(cmd)) |
| 515 | return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals); |
| 516 | if (!PyString_Check(cmd)) { |
| 517 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 518 | "eval() argument 1 must be string or code object"); |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 519 | return NULL; |
| 520 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 521 | str = PyString_AsString(cmd); |
| 522 | if ((int)strlen(str) != PyString_Size(cmd)) { |
| 523 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 524 | "embedded '\\0' in string arg"); |
| 525 | return NULL; |
Guido van Rossum | f08ab0a | 1992-03-04 16:41:41 +0000 | [diff] [blame] | 526 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 527 | while (*str == ' ' || *str == '\t') |
| 528 | str++; |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 529 | return PyRun_String(str, Py_eval_input, globals, locals); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 532 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 533 | builtin_execfile(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 534 | PyObject *self; |
| 535 | PyObject *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 536 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 537 | char *filename; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 538 | PyObject *globals = Py_None, *locals = Py_None; |
| 539 | PyObject *res; |
Guido van Rossum | 0f61f8a | 1992-02-25 18:55:05 +0000 | [diff] [blame] | 540 | FILE* fp; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 541 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 542 | if (!PyArg_ParseTuple(args, "s|O!O!:execfile", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 543 | &filename, |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 544 | &PyDict_Type, &globals, |
| 545 | &PyDict_Type, &locals)) |
Guido van Rossum | 0f61f8a | 1992-02-25 18:55:05 +0000 | [diff] [blame] | 546 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 547 | if (globals == Py_None) { |
| 548 | globals = PyEval_GetGlobals(); |
| 549 | if (locals == Py_None) |
| 550 | locals = PyEval_GetLocals(); |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 551 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 552 | else if (locals == Py_None) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 553 | locals = globals; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 554 | if (PyDict_GetItemString(globals, "__builtins__") == NULL) { |
| 555 | if (PyDict_SetItemString(globals, "__builtins__", |
| 556 | PyEval_GetBuiltins()) != 0) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 557 | return NULL; |
| 558 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 559 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 560 | fp = fopen(filename, "r"); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 561 | Py_END_ALLOW_THREADS |
Guido van Rossum | 0f61f8a | 1992-02-25 18:55:05 +0000 | [diff] [blame] | 562 | if (fp == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 563 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 0f61f8a | 1992-02-25 18:55:05 +0000 | [diff] [blame] | 564 | return NULL; |
| 565 | } |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 566 | res = PyRun_File(fp, filename, Py_file_input, globals, locals); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 567 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 0f61f8a | 1992-02-25 18:55:05 +0000 | [diff] [blame] | 568 | fclose(fp); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 569 | Py_END_ALLOW_THREADS |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 570 | return res; |
Guido van Rossum | 0f61f8a | 1992-02-25 18:55:05 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 573 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 574 | builtin_float(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 575 | PyObject *self; |
| 576 | PyObject *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 577 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 578 | PyObject *v; |
| 579 | PyNumberMethods *nb; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 580 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 581 | if (!PyArg_ParseTuple(args, "O:float", &v)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 582 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 583 | if (PyString_Check(v)) |
Guido van Rossum | c6472e9 | 1997-03-31 17:15:43 +0000 | [diff] [blame] | 584 | return float_from_string(v); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 585 | if ((nb = v->ob_type->tp_as_number) == NULL || |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 586 | nb->nb_float == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 587 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 588 | "float() argument can't be converted to float"); |
| 589 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 590 | } |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 591 | return (*nb->nb_float)(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 594 | static PyObject * |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 595 | builtin_getattr(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 596 | PyObject *self; |
| 597 | PyObject *args; |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 598 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 599 | PyObject *v; |
| 600 | PyObject *name; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 601 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 602 | if (!PyArg_ParseTuple(args, "OS:getattr", &v, &name)) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 603 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 604 | return PyObject_GetAttr(v, name); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 607 | static PyObject * |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 608 | builtin_globals(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 609 | PyObject *self; |
| 610 | PyObject *args; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 611 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 612 | PyObject *d; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 613 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 614 | if (!PyArg_ParseTuple(args, "")) |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 615 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 616 | d = PyEval_GetGlobals(); |
| 617 | Py_INCREF(d); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 618 | return d; |
| 619 | } |
| 620 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 621 | static PyObject * |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 622 | builtin_hasattr(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 623 | PyObject *self; |
| 624 | PyObject *args; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 625 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 626 | PyObject *v; |
| 627 | PyObject *name; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 628 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 629 | if (!PyArg_ParseTuple(args, "OS:hasattr", &v, &name)) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 630 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 631 | v = PyObject_GetAttr(v, name); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 632 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 633 | PyErr_Clear(); |
| 634 | return PyInt_FromLong(0L); |
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 | Py_DECREF(v); |
| 637 | return PyInt_FromLong(1L); |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 640 | static PyObject * |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 641 | builtin_id(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 642 | PyObject *self; |
| 643 | PyObject *args; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 644 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 645 | PyObject *v; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 646 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 647 | if (!PyArg_ParseTuple(args, "O:id", &v)) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 648 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 649 | return PyInt_FromLong((long)v); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 652 | static PyObject * |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 653 | builtin_map(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 654 | PyObject *self; |
| 655 | PyObject *args; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 656 | { |
| 657 | typedef struct { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 658 | PyObject *seq; |
| 659 | PySequenceMethods *sqf; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 660 | int len; |
| 661 | } sequence; |
| 662 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 663 | PyObject *func, *result; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 664 | sequence *seqs = NULL, *sqp; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 665 | int n, len; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 666 | register int i, j; |
| 667 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 668 | n = PyTuple_Size(args); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 669 | if (n < 2) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 670 | PyErr_SetString(PyExc_TypeError, |
| 671 | "map() requires at least two args"); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 672 | return NULL; |
| 673 | } |
| 674 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 675 | func = PyTuple_GetItem(args, 0); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 676 | n--; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 677 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 678 | if ((seqs = PyMem_NEW(sequence, n)) == NULL) { |
| 679 | PyErr_NoMemory(); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 680 | goto Fail_2; |
| 681 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 682 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 683 | for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) { |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 684 | int curlen; |
| 685 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 686 | if ((sqp->seq = PyTuple_GetItem(args, i + 1)) == NULL) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 687 | goto Fail_2; |
| 688 | |
| 689 | if (! (sqp->sqf = sqp->seq->ob_type->tp_as_sequence)) { |
| 690 | static char errmsg[] = |
| 691 | "argument %d to map() must be a sequence object"; |
Guido van Rossum | 15e33a4 | 1997-04-30 19:00:27 +0000 | [diff] [blame] | 692 | char errbuf[sizeof(errmsg) + 25]; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 693 | |
| 694 | sprintf(errbuf, errmsg, i+2); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 695 | PyErr_SetString(PyExc_TypeError, errbuf); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 696 | goto Fail_2; |
| 697 | } |
| 698 | |
| 699 | if ((curlen = sqp->len = (*sqp->sqf->sq_length)(sqp->seq)) < 0) |
| 700 | goto Fail_2; |
| 701 | |
| 702 | if (curlen > len) |
| 703 | len = curlen; |
| 704 | } |
| 705 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 706 | if ((result = (PyObject *) PyList_New(len)) == NULL) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 707 | goto Fail_2; |
| 708 | |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 709 | /* XXX Special case map(None, single_list) could be more efficient */ |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 710 | for (i = 0; ; ++i) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 711 | PyObject *alist, *item=NULL, *value; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 712 | int any = 0; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 713 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 714 | if (func == Py_None && n == 1) |
Guido van Rossum | 3212031 | 1995-07-10 13:52:21 +0000 | [diff] [blame] | 715 | alist = NULL; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 716 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 717 | if ((alist = PyTuple_New(n)) == NULL) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 718 | goto Fail_1; |
| 719 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 720 | |
| 721 | for (j = 0, sqp = seqs; j < n; ++j, ++sqp) { |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 722 | if (sqp->len < 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 723 | Py_INCREF(Py_None); |
| 724 | item = Py_None; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 725 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 726 | else { |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 727 | item = (*sqp->sqf->sq_item)(sqp->seq, i); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 728 | if (item == NULL) { |
| 729 | if (i < sqp->len) |
| 730 | goto Fail_0; |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 731 | if (PyErr_ExceptionMatches( |
| 732 | PyExc_IndexError)) |
| 733 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 734 | PyErr_Clear(); |
| 735 | Py_INCREF(Py_None); |
| 736 | item = Py_None; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 737 | sqp->len = -1; |
| 738 | } |
| 739 | else { |
| 740 | goto Fail_0; |
| 741 | } |
| 742 | } |
| 743 | else |
| 744 | any = 1; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 745 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 746 | } |
Guido van Rossum | 3212031 | 1995-07-10 13:52:21 +0000 | [diff] [blame] | 747 | if (!alist) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 748 | break; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 749 | if (PyTuple_SetItem(alist, j, item) < 0) { |
| 750 | Py_DECREF(item); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 751 | goto Fail_0; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 752 | } |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 753 | continue; |
| 754 | |
| 755 | Fail_0: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 756 | Py_XDECREF(alist); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 757 | goto Fail_1; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Guido van Rossum | 3212031 | 1995-07-10 13:52:21 +0000 | [diff] [blame] | 760 | if (!alist) |
| 761 | alist = item; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 762 | |
| 763 | if (!any) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 764 | Py_DECREF(alist); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 765 | break; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 766 | } |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 767 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 768 | if (func == Py_None) |
Guido van Rossum | 3212031 | 1995-07-10 13:52:21 +0000 | [diff] [blame] | 769 | value = alist; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 770 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 771 | value = PyEval_CallObject(func, alist); |
| 772 | Py_DECREF(alist); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 773 | if (value == NULL) |
| 774 | goto Fail_1; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 775 | } |
| 776 | if (i >= len) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 777 | if (PyList_Append(result, value) < 0) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 778 | goto Fail_1; |
| 779 | } |
| 780 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 781 | if (PyList_SetItem(result, i, value) < 0) |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 782 | goto Fail_1; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 786 | PyMem_DEL(seqs); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 787 | return result; |
| 788 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 789 | Fail_1: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 790 | Py_DECREF(result); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 791 | Fail_2: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 792 | if (seqs) PyMem_DEL(seqs); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 793 | return NULL; |
| 794 | } |
| 795 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 796 | static PyObject * |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 797 | builtin_setattr(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 798 | PyObject *self; |
| 799 | PyObject *args; |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 800 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 801 | PyObject *v; |
| 802 | PyObject *name; |
| 803 | PyObject *value; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 804 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 805 | if (!PyArg_ParseTuple(args, "OSO:setattr", &v, &name, &value)) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 806 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 807 | if (PyObject_SetAttr(v, name, value) != 0) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 808 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 809 | Py_INCREF(Py_None); |
| 810 | return Py_None; |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 811 | } |
| 812 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 813 | static PyObject * |
Guido van Rossum | 14144fc | 1994-08-29 12:53:40 +0000 | [diff] [blame] | 814 | builtin_delattr(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 815 | PyObject *self; |
| 816 | PyObject *args; |
Guido van Rossum | 14144fc | 1994-08-29 12:53:40 +0000 | [diff] [blame] | 817 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 818 | PyObject *v; |
| 819 | PyObject *name; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 820 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 821 | if (!PyArg_ParseTuple(args, "OS:delattr", &v, &name)) |
Guido van Rossum | 14144fc | 1994-08-29 12:53:40 +0000 | [diff] [blame] | 822 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 823 | if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0) |
Guido van Rossum | 14144fc | 1994-08-29 12:53:40 +0000 | [diff] [blame] | 824 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 825 | Py_INCREF(Py_None); |
| 826 | return Py_None; |
Guido van Rossum | 14144fc | 1994-08-29 12:53:40 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 829 | static PyObject * |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 830 | builtin_hash(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 831 | PyObject *self; |
| 832 | PyObject *args; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 833 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 834 | PyObject *v; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 835 | long x; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 836 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 837 | if (!PyArg_ParseTuple(args, "O:hash", &v)) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 838 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 839 | x = PyObject_Hash(v); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 840 | if (x == -1) |
| 841 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 842 | return PyInt_FromLong(x); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 845 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 846 | builtin_hex(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 847 | PyObject *self; |
| 848 | PyObject *args; |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 849 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 850 | PyObject *v; |
| 851 | PyNumberMethods *nb; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 852 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 853 | if (!PyArg_ParseTuple(args, "O:hex", &v)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 854 | return NULL; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 855 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 856 | if ((nb = v->ob_type->tp_as_number) == NULL || |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 857 | nb->nb_hex == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 858 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 859 | "hex() argument can't be converted to hex"); |
| 860 | return NULL; |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 861 | } |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 862 | return (*nb->nb_hex)(v); |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 865 | static PyObject *builtin_raw_input Py_PROTO((PyObject *, PyObject *)); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 866 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 867 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 868 | builtin_input(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 869 | PyObject *self; |
| 870 | PyObject *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 871 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 872 | PyObject *line; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 873 | char *str; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 874 | PyObject *res; |
| 875 | PyObject *globals, *locals; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 876 | |
| 877 | line = builtin_raw_input(self, args); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 878 | if (line == NULL) |
| 879 | return line; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 880 | if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 881 | return NULL; |
| 882 | while (*str == ' ' || *str == '\t') |
| 883 | str++; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 884 | globals = PyEval_GetGlobals(); |
| 885 | locals = PyEval_GetLocals(); |
| 886 | if (PyDict_GetItemString(globals, "__builtins__") == NULL) { |
| 887 | if (PyDict_SetItemString(globals, "__builtins__", |
| 888 | PyEval_GetBuiltins()) != 0) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 889 | return NULL; |
| 890 | } |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 891 | res = PyRun_String(str, Py_eval_input, globals, locals); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 892 | Py_DECREF(line); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 893 | return res; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Guido van Rossum | e8811f8 | 1997-02-14 15:48:05 +0000 | [diff] [blame] | 896 | static PyObject * |
| 897 | builtin_intern(self, args) |
| 898 | PyObject *self; |
| 899 | PyObject *args; |
| 900 | { |
| 901 | PyObject *s; |
| 902 | if (!PyArg_ParseTuple(args, "S", &s)) |
| 903 | return NULL; |
| 904 | Py_INCREF(s); |
| 905 | PyString_InternInPlace(&s); |
| 906 | return s; |
| 907 | } |
| 908 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 909 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 910 | builtin_int(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 911 | PyObject *self; |
| 912 | PyObject *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 913 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 914 | PyObject *v; |
| 915 | PyNumberMethods *nb; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 916 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 917 | if (!PyArg_ParseTuple(args, "O:int", &v)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 918 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 919 | if (PyString_Check(v)) |
Guido van Rossum | c6472e9 | 1997-03-31 17:15:43 +0000 | [diff] [blame] | 920 | return int_from_string(v); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 921 | if ((nb = v->ob_type->tp_as_number) == NULL || |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 922 | nb->nb_int == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 923 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 924 | "int() argument can't be converted to int"); |
| 925 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 926 | } |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 927 | return (*nb->nb_int)(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 930 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 931 | builtin_len(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 932 | PyObject *self; |
| 933 | PyObject *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 934 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 935 | PyObject *v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 936 | long len; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 937 | PyTypeObject *tp; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 938 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 939 | if (!PyArg_ParseTuple(args, "O:len", &v)) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 940 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 941 | tp = v->ob_type; |
| 942 | if (tp->tp_as_sequence != NULL) { |
| 943 | len = (*tp->tp_as_sequence->sq_length)(v); |
| 944 | } |
| 945 | else if (tp->tp_as_mapping != NULL) { |
| 946 | len = (*tp->tp_as_mapping->mp_length)(v); |
| 947 | } |
| 948 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 949 | PyErr_SetString(PyExc_TypeError, "len() of unsized object"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 950 | return NULL; |
| 951 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 952 | if (len < 0) |
| 953 | return NULL; |
| 954 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 955 | return PyInt_FromLong(len); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 956 | } |
| 957 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 958 | static PyObject * |
Guido van Rossum | d170577 | 1996-04-09 02:41:06 +0000 | [diff] [blame] | 959 | builtin_list(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 960 | PyObject *self; |
| 961 | PyObject *args; |
Guido van Rossum | d170577 | 1996-04-09 02:41:06 +0000 | [diff] [blame] | 962 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 963 | PyObject *v; |
| 964 | PySequenceMethods *sqf; |
Guido van Rossum | d170577 | 1996-04-09 02:41:06 +0000 | [diff] [blame] | 965 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 966 | if (!PyArg_ParseTuple(args, "O:list", &v)) |
Guido van Rossum | d170577 | 1996-04-09 02:41:06 +0000 | [diff] [blame] | 967 | return NULL; |
| 968 | if ((sqf = v->ob_type->tp_as_sequence) != NULL) { |
| 969 | int n = (*sqf->sq_length)(v); |
| 970 | int i; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 971 | PyObject *l; |
Guido van Rossum | d170577 | 1996-04-09 02:41:06 +0000 | [diff] [blame] | 972 | if (n < 0) |
| 973 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 974 | l = PyList_New(n); |
Guido van Rossum | d170577 | 1996-04-09 02:41:06 +0000 | [diff] [blame] | 975 | if (l == NULL) |
| 976 | return NULL; |
| 977 | for (i = 0; i < n; i++) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 978 | PyObject *item = (*sqf->sq_item)(v, i); |
Guido van Rossum | d170577 | 1996-04-09 02:41:06 +0000 | [diff] [blame] | 979 | if (item == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 980 | Py_DECREF(l); |
Guido van Rossum | d170577 | 1996-04-09 02:41:06 +0000 | [diff] [blame] | 981 | l = NULL; |
| 982 | break; |
| 983 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 984 | PyList_SetItem(l, i, item); |
Guido van Rossum | d170577 | 1996-04-09 02:41:06 +0000 | [diff] [blame] | 985 | } |
| 986 | /* XXX Should support indefinite-length sequences */ |
| 987 | return l; |
| 988 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 989 | PyErr_SetString(PyExc_TypeError, "list() argument must be a sequence"); |
Guido van Rossum | d170577 | 1996-04-09 02:41:06 +0000 | [diff] [blame] | 990 | return NULL; |
| 991 | } |
| 992 | |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 993 | |
| 994 | static PyObject * |
| 995 | builtin_slice(self, args) |
| 996 | PyObject *self; |
| 997 | PyObject *args; |
| 998 | { |
| 999 | PyObject *start, *stop, *step; |
| 1000 | |
| 1001 | start = stop = step = NULL; |
| 1002 | |
| 1003 | if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step)) |
| 1004 | return NULL; |
| 1005 | |
| 1006 | /*This swapping of stop and start is to maintain compatibility with |
| 1007 | the range builtin.*/ |
| 1008 | if (stop == NULL) { |
| 1009 | stop = start; |
| 1010 | start = NULL; |
| 1011 | } |
| 1012 | return PySlice_New(start, stop, step); |
| 1013 | } |
| 1014 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1015 | static PyObject * |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1016 | builtin_locals(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1017 | PyObject *self; |
| 1018 | PyObject *args; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1019 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1020 | PyObject *d; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1021 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1022 | if (!PyArg_ParseTuple(args, "")) |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1023 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1024 | d = PyEval_GetLocals(); |
| 1025 | Py_INCREF(d); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1026 | return d; |
| 1027 | } |
| 1028 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1029 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1030 | builtin_long(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1031 | PyObject *self; |
| 1032 | PyObject *args; |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 1033 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1034 | PyObject *v; |
| 1035 | PyNumberMethods *nb; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1036 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1037 | if (!PyArg_ParseTuple(args, "O:long", &v)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1038 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1039 | if (PyString_Check(v)) |
Guido van Rossum | c6472e9 | 1997-03-31 17:15:43 +0000 | [diff] [blame] | 1040 | return long_from_string(v); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1041 | if ((nb = v->ob_type->tp_as_number) == NULL || |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1042 | nb->nb_long == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1043 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1044 | "long() argument can't be converted to long"); |
| 1045 | return NULL; |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 1046 | } |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1047 | return (*nb->nb_long)(v); |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1050 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1051 | min_max(args, sign) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1052 | PyObject *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1053 | int sign; |
| 1054 | { |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1055 | int i; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1056 | PyObject *v, *w, *x; |
| 1057 | PySequenceMethods *sq; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1058 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1059 | if (PyTuple_Size(args) > 1) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1060 | v = args; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1061 | else if (!PyArg_ParseTuple(args, "O:min/max", &v)) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1062 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1063 | sq = v->ob_type->tp_as_sequence; |
| 1064 | if (sq == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1065 | PyErr_SetString(PyExc_TypeError, |
| 1066 | "min() or max() of non-sequence"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1067 | return NULL; |
| 1068 | } |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1069 | w = NULL; |
| 1070 | for (i = 0; ; i++) { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1071 | x = (*sq->sq_item)(v, i); /* Implies INCREF */ |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1072 | if (x == NULL) { |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1073 | if (PyErr_ExceptionMatches(PyExc_IndexError)) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1074 | PyErr_Clear(); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1075 | break; |
| 1076 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1077 | Py_XDECREF(w); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1078 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1079 | } |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1080 | if (w == NULL) |
| 1081 | w = x; |
| 1082 | else { |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 1083 | int c = PyObject_Compare(x, w); |
| 1084 | if (c && PyErr_Occurred()) { |
| 1085 | Py_DECREF(x); |
| 1086 | Py_XDECREF(w); |
| 1087 | return NULL; |
| 1088 | } |
| 1089 | if (c * sign > 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1090 | Py_DECREF(w); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1091 | w = x; |
| 1092 | } |
| 1093 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1094 | Py_DECREF(x); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1095 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1096 | } |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1097 | if (w == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1098 | PyErr_SetString(PyExc_ValueError, |
| 1099 | "min() or max() of empty sequence"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1100 | return w; |
| 1101 | } |
| 1102 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1103 | static PyObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1104 | builtin_min(self, v) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1105 | PyObject *self; |
| 1106 | PyObject *v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1107 | { |
| 1108 | return min_max(v, -1); |
| 1109 | } |
| 1110 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1111 | static PyObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1112 | builtin_max(self, v) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1113 | PyObject *self; |
| 1114 | PyObject *v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1115 | { |
| 1116 | return min_max(v, 1); |
| 1117 | } |
| 1118 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1119 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1120 | builtin_oct(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1121 | PyObject *self; |
| 1122 | PyObject *args; |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 1123 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1124 | PyObject *v; |
| 1125 | PyNumberMethods *nb; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1126 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1127 | if (!PyArg_ParseTuple(args, "O:oct", &v)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1128 | return NULL; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1129 | if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL || |
| 1130 | nb->nb_oct == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1131 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1132 | "oct() argument can't be converted to oct"); |
| 1133 | return NULL; |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 1134 | } |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1135 | return (*nb->nb_oct)(v); |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1138 | static PyObject * |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 1139 | builtin_open(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1140 | PyObject *self; |
| 1141 | PyObject *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1142 | { |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1143 | char *name; |
| 1144 | char *mode = "r"; |
| 1145 | int bufsize = -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1146 | PyObject *f; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1147 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1148 | if (!PyArg_ParseTuple(args, "s|si:open", &name, &mode, &bufsize)) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1149 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1150 | f = PyFile_FromString(name, mode); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1151 | if (f != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1152 | PyFile_SetBufSize(f, bufsize); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1153 | return f; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1156 | static PyObject * |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 1157 | builtin_ord(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1158 | PyObject *self; |
| 1159 | PyObject *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1160 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1161 | char c; |
| 1162 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1163 | if (!PyArg_ParseTuple(args, "c:ord", &c)) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1164 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1165 | return PyInt_FromLong((long)(c & 0xff)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1168 | static PyObject * |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1169 | do_pow(v, w) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1170 | PyObject *v, *w; |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 1171 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1172 | PyObject *res; |
| 1173 | if (PyInstance_Check(v) || PyInstance_Check(w)) |
| 1174 | return PyInstance_DoBinOp(v, w, "__pow__", "__rpow__", do_pow); |
Guido van Rossum | c6bb8f7 | 1991-07-01 18:42:41 +0000 | [diff] [blame] | 1175 | if (v->ob_type->tp_as_number == NULL || |
Guido van Rossum | df05ac6 | 1994-08-29 12:52:37 +0000 | [diff] [blame] | 1176 | w->ob_type->tp_as_number == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1177 | PyErr_SetString(PyExc_TypeError, |
| 1178 | "pow() requires numeric arguments"); |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 1179 | return NULL; |
| 1180 | } |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 1181 | if ( |
| 1182 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1183 | !PyComplex_Check(v) && |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 1184 | #endif |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1185 | PyFloat_Check(w) && PyFloat_AsDouble(v) < 0.0) { |
| 1186 | if (!PyErr_Occurred()) |
| 1187 | PyErr_SetString(PyExc_ValueError, |
| 1188 | "negative number to float power"); |
Guido van Rossum | 2165158 | 1995-02-10 16:57:16 +0000 | [diff] [blame] | 1189 | return NULL; |
| 1190 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1191 | if (PyNumber_Coerce(&v, &w) != 0) |
Guido van Rossum | c6bb8f7 | 1991-07-01 18:42:41 +0000 | [diff] [blame] | 1192 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1193 | res = (*v->ob_type->tp_as_number->nb_power)(v, w, Py_None); |
| 1194 | Py_DECREF(v); |
| 1195 | Py_DECREF(w); |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1196 | return res; |
| 1197 | } |
| 1198 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1199 | static PyObject * |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1200 | builtin_pow(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1201 | PyObject *self; |
| 1202 | PyObject *args; |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1203 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1204 | PyObject *v, *w, *z = Py_None, *res; |
| 1205 | PyObject *v1, *z1, *w2, *z2; |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1206 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1207 | if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z)) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1208 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1209 | if (z == Py_None) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1210 | return do_pow(v, w); |
| 1211 | /* XXX The ternary version doesn't do class instance coercions */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1212 | if (PyInstance_Check(v)) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1213 | return v->ob_type->tp_as_number->nb_power(v, w, z); |
| 1214 | if (v->ob_type->tp_as_number == NULL || |
| 1215 | z->ob_type->tp_as_number == NULL || |
| 1216 | w->ob_type->tp_as_number == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1217 | PyErr_SetString(PyExc_TypeError, |
| 1218 | "pow() requires numeric arguments"); |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1219 | return NULL; |
| 1220 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1221 | if (PyNumber_Coerce(&v, &w) != 0) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1222 | return NULL; |
| 1223 | res = NULL; |
| 1224 | v1 = v; |
| 1225 | z1 = z; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1226 | if (PyNumber_Coerce(&v1, &z1) != 0) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1227 | goto error2; |
| 1228 | w2 = w; |
| 1229 | z2 = z1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1230 | if (PyNumber_Coerce(&w2, &z2) != 0) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1231 | goto error1; |
| 1232 | res = (*v1->ob_type->tp_as_number->nb_power)(v1, w2, z2); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1233 | Py_DECREF(w2); |
| 1234 | Py_DECREF(z2); |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1235 | error1: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1236 | Py_DECREF(v1); |
| 1237 | Py_DECREF(z1); |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1238 | error2: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1239 | Py_DECREF(v); |
| 1240 | Py_DECREF(w); |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1241 | return res; |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1244 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1245 | builtin_range(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1246 | PyObject *self; |
| 1247 | PyObject *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1248 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1249 | long ilow = 0, ihigh = 0, istep = 1; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1250 | int i, n; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1251 | PyObject *v; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1252 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1253 | if (PyTuple_Size(args) <= 1) { |
| 1254 | if (!PyArg_ParseTuple(args, |
Guido van Rossum | 0865dd9 | 1995-01-17 16:30:22 +0000 | [diff] [blame] | 1255 | "l;range() requires 1-3 int arguments", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1256 | &ihigh)) |
| 1257 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1258 | } |
| 1259 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1260 | if (!PyArg_ParseTuple(args, |
Guido van Rossum | 0865dd9 | 1995-01-17 16:30:22 +0000 | [diff] [blame] | 1261 | "ll|l;range() requires 1-3 int arguments", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1262 | &ilow, &ihigh, &istep)) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1263 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1264 | } |
| 1265 | if (istep == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1266 | PyErr_SetString(PyExc_ValueError, "zero step for range()"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1267 | return NULL; |
| 1268 | } |
| 1269 | /* XXX ought to check overflow of subtraction */ |
| 1270 | if (istep > 0) |
| 1271 | n = (ihigh - ilow + istep - 1) / istep; |
| 1272 | else |
| 1273 | n = (ihigh - ilow + istep + 1) / istep; |
| 1274 | if (n < 0) |
| 1275 | n = 0; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1276 | v = PyList_New(n); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1277 | if (v == NULL) |
| 1278 | return NULL; |
| 1279 | for (i = 0; i < n; i++) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1280 | PyObject *w = PyInt_FromLong(ilow); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1281 | if (w == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1282 | Py_DECREF(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1283 | return NULL; |
| 1284 | } |
Guido van Rossum | a937d14 | 1998-04-24 18:22:02 +0000 | [diff] [blame] | 1285 | PyList_SET_ITEM(v, i, w); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1286 | ilow += istep; |
| 1287 | } |
| 1288 | return v; |
| 1289 | } |
| 1290 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1291 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1292 | builtin_xrange(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1293 | PyObject *self; |
| 1294 | PyObject *args; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1295 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1296 | long ilow = 0, ihigh = 0, istep = 1; |
Guido van Rossum | 0865dd9 | 1995-01-17 16:30:22 +0000 | [diff] [blame] | 1297 | long n; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1298 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1299 | if (PyTuple_Size(args) <= 1) { |
| 1300 | if (!PyArg_ParseTuple(args, |
Guido van Rossum | 0865dd9 | 1995-01-17 16:30:22 +0000 | [diff] [blame] | 1301 | "l;xrange() requires 1-3 int arguments", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1302 | &ihigh)) |
| 1303 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1304 | } |
| 1305 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1306 | if (!PyArg_ParseTuple(args, |
Guido van Rossum | 0865dd9 | 1995-01-17 16:30:22 +0000 | [diff] [blame] | 1307 | "ll|l;xrange() requires 1-3 int arguments", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1308 | &ilow, &ihigh, &istep)) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1309 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1310 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1311 | if (istep == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1312 | PyErr_SetString(PyExc_ValueError, "zero step for xrange()"); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1313 | return NULL; |
| 1314 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1315 | /* XXX ought to check overflow of subtraction */ |
| 1316 | if (istep > 0) |
| 1317 | n = (ihigh - ilow + istep - 1) / istep; |
| 1318 | else |
| 1319 | n = (ihigh - ilow + istep + 1) / istep; |
| 1320 | if (n < 0) |
| 1321 | n = 0; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1322 | return PyRange_New(ilow, n, istep, 1); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1325 | extern char *PyOS_Readline Py_PROTO((char *)); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1326 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1327 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1328 | builtin_raw_input(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1329 | PyObject *self; |
| 1330 | PyObject *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1331 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1332 | PyObject *v = NULL; |
| 1333 | PyObject *f; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1334 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1335 | if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v)) |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1336 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1337 | if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin && |
| 1338 | PyFile_AsFile(PySys_GetObject("stdout")) == stdout && |
Guido van Rossum | 53bb7ff | 1995-07-26 16:26:31 +0000 | [diff] [blame] | 1339 | isatty(fileno(stdin)) && isatty(fileno(stdout))) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1340 | PyObject *po; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1341 | char *prompt; |
| 1342 | char *s; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1343 | PyObject *result; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1344 | if (v != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1345 | po = PyObject_Str(v); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1346 | if (po == NULL) |
| 1347 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1348 | prompt = PyString_AsString(po); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1349 | } |
| 1350 | else { |
| 1351 | po = NULL; |
| 1352 | prompt = ""; |
| 1353 | } |
Guido van Rossum | ee81af8 | 1997-09-26 21:47:43 +0000 | [diff] [blame] | 1354 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1355 | s = PyOS_Readline(prompt); |
Guido van Rossum | ee81af8 | 1997-09-26 21:47:43 +0000 | [diff] [blame] | 1356 | Py_END_ALLOW_THREADS |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1357 | Py_XDECREF(po); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1358 | if (s == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1359 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1360 | return NULL; |
| 1361 | } |
| 1362 | if (*s == '\0') { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1363 | PyErr_SetNone(PyExc_EOFError); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1364 | result = NULL; |
| 1365 | } |
| 1366 | else { /* strip trailing '\n' */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1367 | result = PyString_FromStringAndSize(s, strlen(s)-1); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1368 | } |
| 1369 | free(s); |
| 1370 | return result; |
| 1371 | } |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 1372 | if (v != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1373 | f = PySys_GetObject("stdout"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1374 | if (f == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1375 | PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1376 | return NULL; |
| 1377 | } |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 1378 | if (Py_FlushLine() != 0 || |
| 1379 | PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0) |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 1380 | return NULL; |
| 1381 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1382 | f = PySys_GetObject("stdin"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1383 | if (f == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1384 | PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1385 | return NULL; |
| 1386 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1387 | return PyFile_GetLine(f, -1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1390 | static PyObject * |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1391 | builtin_reduce(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1392 | PyObject *self; |
| 1393 | PyObject *args; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1394 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1395 | PyObject *seq, *func, *result = NULL; |
| 1396 | PySequenceMethods *sqf; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1397 | register int i; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1398 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1399 | if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1400 | return NULL; |
| 1401 | if (result != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1402 | Py_INCREF(result); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1403 | |
| 1404 | if ((sqf = seq->ob_type->tp_as_sequence) == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1405 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1406 | "2nd argument to reduce() must be a sequence object"); |
| 1407 | return NULL; |
| 1408 | } |
| 1409 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1410 | if ((args = PyTuple_New(2)) == NULL) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1411 | goto Fail; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1412 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1413 | for (i = 0; ; ++i) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1414 | PyObject *op2; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1415 | |
| 1416 | if (args->ob_refcnt > 1) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1417 | Py_DECREF(args); |
| 1418 | if ((args = PyTuple_New(2)) == NULL) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1419 | goto Fail; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1422 | if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) { |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1423 | if (PyErr_ExceptionMatches(PyExc_IndexError)) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1424 | PyErr_Clear(); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1425 | break; |
| 1426 | } |
| 1427 | goto Fail; |
| 1428 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1429 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1430 | if (result == NULL) |
| 1431 | result = op2; |
| 1432 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1433 | PyTuple_SetItem(args, 0, result); |
| 1434 | PyTuple_SetItem(args, 1, op2); |
| 1435 | if ((result = PyEval_CallObject(func, args)) == NULL) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1436 | goto Fail; |
| 1437 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1440 | Py_DECREF(args); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1441 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1442 | if (result == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1443 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1444 | "reduce of empty sequence with no initial value"); |
| 1445 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1446 | return result; |
| 1447 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1448 | Fail: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1449 | Py_XDECREF(args); |
| 1450 | Py_XDECREF(result); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1451 | return NULL; |
| 1452 | } |
| 1453 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1454 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1455 | builtin_reload(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1456 | PyObject *self; |
| 1457 | PyObject *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1458 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1459 | PyObject *v; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1460 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1461 | if (!PyArg_ParseTuple(args, "O:reload", &v)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1462 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1463 | return PyImport_ReloadModule(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1466 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1467 | builtin_repr(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1468 | PyObject *self; |
| 1469 | PyObject *args; |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1470 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1471 | PyObject *v; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1472 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1473 | if (!PyArg_ParseTuple(args, "O:repr", &v)) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1474 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1475 | return PyObject_Repr(v); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1478 | static PyObject * |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1479 | builtin_round(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1480 | PyObject *self; |
| 1481 | PyObject *args; |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1482 | { |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1483 | double x; |
| 1484 | double f; |
| 1485 | int ndigits = 0; |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1486 | int i; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1487 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1488 | if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits)) |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1489 | return NULL; |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1490 | f = 1.0; |
| 1491 | for (i = ndigits; --i >= 0; ) |
| 1492 | f = f*10.0; |
| 1493 | for (i = ndigits; ++i <= 0; ) |
| 1494 | f = f*0.1; |
| 1495 | if (x >= 0.0) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1496 | return PyFloat_FromDouble(floor(x*f + 0.5) / f); |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1497 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1498 | return PyFloat_FromDouble(ceil(x*f - 0.5) / f); |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1501 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1502 | builtin_str(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1503 | PyObject *self; |
| 1504 | PyObject *args; |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1505 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1506 | PyObject *v; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1507 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1508 | if (!PyArg_ParseTuple(args, "O:str", &v)) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1509 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1510 | return PyObject_Str(v); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1513 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1514 | builtin_tuple(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1515 | PyObject *self; |
| 1516 | PyObject *args; |
Guido van Rossum | cae027b | 1994-08-29 12:53:11 +0000 | [diff] [blame] | 1517 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1518 | PyObject *v; |
| 1519 | PySequenceMethods *sqf; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1520 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1521 | if (!PyArg_ParseTuple(args, "O:tuple", &v)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1522 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1523 | if (PyTuple_Check(v)) { |
| 1524 | Py_INCREF(v); |
Guido van Rossum | cae027b | 1994-08-29 12:53:11 +0000 | [diff] [blame] | 1525 | return v; |
| 1526 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1527 | if (PyList_Check(v)) |
| 1528 | return PyList_AsTuple(v); |
| 1529 | if (PyString_Check(v)) { |
| 1530 | int n = PyString_Size(v); |
| 1531 | PyObject *t = PyTuple_New(n); |
Guido van Rossum | cae027b | 1994-08-29 12:53:11 +0000 | [diff] [blame] | 1532 | if (t != NULL) { |
| 1533 | int i; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1534 | char *p = PyString_AsString(v); |
Guido van Rossum | cae027b | 1994-08-29 12:53:11 +0000 | [diff] [blame] | 1535 | for (i = 0; i < n; i++) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1536 | PyObject *item = |
| 1537 | PyString_FromStringAndSize(p+i, 1); |
Guido van Rossum | cae027b | 1994-08-29 12:53:11 +0000 | [diff] [blame] | 1538 | if (item == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1539 | Py_DECREF(t); |
Guido van Rossum | cae027b | 1994-08-29 12:53:11 +0000 | [diff] [blame] | 1540 | t = NULL; |
| 1541 | break; |
| 1542 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1543 | PyTuple_SetItem(t, i, item); |
Guido van Rossum | cae027b | 1994-08-29 12:53:11 +0000 | [diff] [blame] | 1544 | } |
| 1545 | } |
| 1546 | return t; |
| 1547 | } |
| 1548 | /* Generic sequence object */ |
| 1549 | if ((sqf = v->ob_type->tp_as_sequence) != NULL) { |
| 1550 | int n = (*sqf->sq_length)(v); |
| 1551 | int i; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1552 | PyObject *t; |
Guido van Rossum | cae027b | 1994-08-29 12:53:11 +0000 | [diff] [blame] | 1553 | if (n < 0) |
| 1554 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1555 | t = PyTuple_New(n); |
Guido van Rossum | cae027b | 1994-08-29 12:53:11 +0000 | [diff] [blame] | 1556 | if (t == NULL) |
| 1557 | return NULL; |
| 1558 | for (i = 0; i < n; i++) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1559 | PyObject *item = (*sqf->sq_item)(v, i); |
Guido van Rossum | cae027b | 1994-08-29 12:53:11 +0000 | [diff] [blame] | 1560 | if (item == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1561 | Py_DECREF(t); |
Guido van Rossum | cae027b | 1994-08-29 12:53:11 +0000 | [diff] [blame] | 1562 | t = NULL; |
| 1563 | break; |
| 1564 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1565 | PyTuple_SetItem(t, i, item); |
Guido van Rossum | cae027b | 1994-08-29 12:53:11 +0000 | [diff] [blame] | 1566 | } |
| 1567 | /* XXX Should support indefinite-length sequences */ |
| 1568 | return t; |
| 1569 | } |
| 1570 | /* None of the above */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1571 | PyErr_SetString(PyExc_TypeError, |
| 1572 | "tuple() argument must be a sequence"); |
Guido van Rossum | cae027b | 1994-08-29 12:53:11 +0000 | [diff] [blame] | 1573 | return NULL; |
| 1574 | } |
| 1575 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1576 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1577 | builtin_type(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1578 | PyObject *self; |
| 1579 | PyObject *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1580 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1581 | PyObject *v; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1582 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1583 | if (!PyArg_ParseTuple(args, "O:type", &v)) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1584 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1585 | v = (PyObject *)v->ob_type; |
| 1586 | Py_INCREF(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1587 | return v; |
| 1588 | } |
| 1589 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1590 | static PyObject * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1591 | builtin_vars(self, args) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1592 | PyObject *self; |
| 1593 | PyObject *args; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1594 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1595 | PyObject *v = NULL; |
| 1596 | PyObject *d; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1597 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1598 | if (!PyArg_ParseTuple(args, "|O:vars", &v)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1599 | return NULL; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1600 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1601 | d = PyEval_GetLocals(); |
Guido van Rossum | 53bb7ff | 1995-07-26 16:26:31 +0000 | [diff] [blame] | 1602 | if (d == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1603 | if (!PyErr_Occurred()) |
| 1604 | PyErr_SetString(PyExc_SystemError, |
| 1605 | "no locals!?"); |
Guido van Rossum | 53bb7ff | 1995-07-26 16:26:31 +0000 | [diff] [blame] | 1606 | } |
| 1607 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1608 | Py_INCREF(d); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1609 | } |
| 1610 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1611 | d = PyObject_GetAttrString(v, "__dict__"); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1612 | if (d == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1613 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1614 | "vars() argument must have __dict__ attribute"); |
| 1615 | return NULL; |
| 1616 | } |
| 1617 | } |
| 1618 | return d; |
| 1619 | } |
| 1620 | |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1621 | static PyObject * |
| 1622 | builtin_isinstance(self, args) |
| 1623 | PyObject *self; |
| 1624 | PyObject *args; |
| 1625 | { |
| 1626 | PyObject *inst; |
| 1627 | PyObject *cls; |
| 1628 | int retval; |
| 1629 | |
| 1630 | if (!PyArg_ParseTuple(args, "OO", &inst, &cls)) |
| 1631 | return NULL; |
Guido van Rossum | f5dd914 | 1997-12-02 19:11:45 +0000 | [diff] [blame] | 1632 | if (PyType_Check(cls)) { |
Guido van Rossum | d6af46d | 1997-12-10 05:51:47 +0000 | [diff] [blame] | 1633 | retval = ((PyObject *)(inst->ob_type) == cls); |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1634 | } |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1635 | else { |
Guido van Rossum | f5dd914 | 1997-12-02 19:11:45 +0000 | [diff] [blame] | 1636 | if (!PyClass_Check(cls)) { |
| 1637 | PyErr_SetString(PyExc_TypeError, |
| 1638 | "second argument must be a class"); |
| 1639 | return NULL; |
| 1640 | } |
| 1641 | |
| 1642 | if (!PyInstance_Check(inst)) |
| 1643 | retval = 0; |
| 1644 | else { |
| 1645 | PyObject *inclass = |
| 1646 | (PyObject*)((PyInstanceObject*)inst)->in_class; |
| 1647 | retval = PyClass_IsSubclass(inclass, cls); |
| 1648 | } |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1649 | } |
| 1650 | return PyInt_FromLong(retval); |
| 1651 | } |
| 1652 | |
| 1653 | |
| 1654 | static PyObject * |
| 1655 | builtin_issubclass(self, args) |
| 1656 | PyObject *self; |
| 1657 | PyObject *args; |
| 1658 | { |
| 1659 | PyObject *derived; |
| 1660 | PyObject *cls; |
| 1661 | int retval; |
| 1662 | |
| 1663 | if (!PyArg_ParseTuple(args, "OO", &derived, &cls)) |
| 1664 | return NULL; |
| 1665 | if (!PyClass_Check(derived) || !PyClass_Check(cls)) { |
| 1666 | PyErr_SetString(PyExc_TypeError, "arguments must be classes"); |
| 1667 | return NULL; |
| 1668 | } |
| 1669 | /* shortcut */ |
| 1670 | if (!(retval = (derived == cls))) |
| 1671 | retval = PyClass_IsSubclass(derived, cls); |
| 1672 | |
| 1673 | return PyInt_FromLong(retval); |
| 1674 | } |
| 1675 | |
| 1676 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1677 | static PyMethodDef builtin_methods[] = { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1678 | {"__import__", builtin___import__, 1}, |
| 1679 | {"abs", builtin_abs, 1}, |
| 1680 | {"apply", builtin_apply, 1}, |
| 1681 | {"callable", builtin_callable, 1}, |
| 1682 | {"chr", builtin_chr, 1}, |
| 1683 | {"cmp", builtin_cmp, 1}, |
| 1684 | {"coerce", builtin_coerce, 1}, |
| 1685 | {"compile", builtin_compile, 1}, |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 1686 | #ifndef WITHOUT_COMPLEX |
| 1687 | {"complex", builtin_complex, 1}, |
| 1688 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1689 | {"delattr", builtin_delattr, 1}, |
| 1690 | {"dir", builtin_dir, 1}, |
| 1691 | {"divmod", builtin_divmod, 1}, |
| 1692 | {"eval", builtin_eval, 1}, |
| 1693 | {"execfile", builtin_execfile, 1}, |
| 1694 | {"filter", builtin_filter, 1}, |
| 1695 | {"float", builtin_float, 1}, |
| 1696 | {"getattr", builtin_getattr, 1}, |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1697 | {"globals", builtin_globals, 1}, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1698 | {"hasattr", builtin_hasattr, 1}, |
| 1699 | {"hash", builtin_hash, 1}, |
| 1700 | {"hex", builtin_hex, 1}, |
| 1701 | {"id", builtin_id, 1}, |
| 1702 | {"input", builtin_input, 1}, |
Guido van Rossum | e8811f8 | 1997-02-14 15:48:05 +0000 | [diff] [blame] | 1703 | {"intern", builtin_intern, 1}, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1704 | {"int", builtin_int, 1}, |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1705 | {"isinstance", builtin_isinstance, 1}, |
| 1706 | {"issubclass", builtin_issubclass, 1}, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1707 | {"len", builtin_len, 1}, |
Guido van Rossum | d170577 | 1996-04-09 02:41:06 +0000 | [diff] [blame] | 1708 | {"list", builtin_list, 1}, |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1709 | {"locals", builtin_locals, 1}, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1710 | {"long", builtin_long, 1}, |
| 1711 | {"map", builtin_map, 1}, |
| 1712 | {"max", builtin_max, 1}, |
| 1713 | {"min", builtin_min, 1}, |
| 1714 | {"oct", builtin_oct, 1}, |
| 1715 | {"open", builtin_open, 1}, |
| 1716 | {"ord", builtin_ord, 1}, |
Guido van Rossum | 030ae17 | 1994-11-10 22:33:19 +0000 | [diff] [blame] | 1717 | {"pow", builtin_pow, 1}, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1718 | {"range", builtin_range, 1}, |
| 1719 | {"raw_input", builtin_raw_input, 1}, |
| 1720 | {"reduce", builtin_reduce, 1}, |
| 1721 | {"reload", builtin_reload, 1}, |
| 1722 | {"repr", builtin_repr, 1}, |
| 1723 | {"round", builtin_round, 1}, |
| 1724 | {"setattr", builtin_setattr, 1}, |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1725 | {"slice", builtin_slice, 1}, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1726 | {"str", builtin_str, 1}, |
| 1727 | {"tuple", builtin_tuple, 1}, |
| 1728 | {"type", builtin_type, 1}, |
| 1729 | {"vars", builtin_vars, 1}, |
| 1730 | {"xrange", builtin_xrange, 1}, |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 1731 | {NULL, NULL}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1732 | }; |
| 1733 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1734 | /* Predefined exceptions */ |
| 1735 | |
Guido van Rossum | 0474832 | 1997-09-16 18:43:15 +0000 | [diff] [blame] | 1736 | PyObject *PyExc_Exception; |
Barry Warsaw | 757af0e | 1997-08-29 22:13:51 +0000 | [diff] [blame] | 1737 | PyObject *PyExc_StandardError; |
Barry Warsaw | 412cdc2 | 1997-09-16 21:51:14 +0000 | [diff] [blame] | 1738 | PyObject *PyExc_ArithmeticError; |
Barry Warsaw | 757af0e | 1997-08-29 22:13:51 +0000 | [diff] [blame] | 1739 | PyObject *PyExc_LookupError; |
| 1740 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1741 | PyObject *PyExc_AssertionError; |
| 1742 | PyObject *PyExc_AttributeError; |
| 1743 | PyObject *PyExc_EOFError; |
Guido van Rossum | b6a7f77 | 1997-05-09 03:03:23 +0000 | [diff] [blame] | 1744 | PyObject *PyExc_FloatingPointError; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1745 | PyObject *PyExc_IOError; |
| 1746 | PyObject *PyExc_ImportError; |
| 1747 | PyObject *PyExc_IndexError; |
| 1748 | PyObject *PyExc_KeyError; |
| 1749 | PyObject *PyExc_KeyboardInterrupt; |
| 1750 | PyObject *PyExc_MemoryError; |
| 1751 | PyObject *PyExc_NameError; |
| 1752 | PyObject *PyExc_OverflowError; |
| 1753 | PyObject *PyExc_RuntimeError; |
| 1754 | PyObject *PyExc_SyntaxError; |
| 1755 | PyObject *PyExc_SystemError; |
| 1756 | PyObject *PyExc_SystemExit; |
| 1757 | PyObject *PyExc_TypeError; |
| 1758 | PyObject *PyExc_ValueError; |
| 1759 | PyObject *PyExc_ZeroDivisionError; |
Guido van Rossum | 50afb7a | 1991-12-10 13:52:31 +0000 | [diff] [blame] | 1760 | |
Barry Warsaw | 757af0e | 1997-08-29 22:13:51 +0000 | [diff] [blame] | 1761 | PyObject *PyExc_MemoryErrorInst; |
| 1762 | |
| 1763 | static struct |
| 1764 | { |
| 1765 | char* name; |
| 1766 | PyObject** exc; |
| 1767 | int leaf_exc; |
| 1768 | } |
| 1769 | bltin_exc[] = { |
Guido van Rossum | 0474832 | 1997-09-16 18:43:15 +0000 | [diff] [blame] | 1770 | {"Exception", &PyExc_Exception, 0}, |
Barry Warsaw | 757af0e | 1997-08-29 22:13:51 +0000 | [diff] [blame] | 1771 | {"StandardError", &PyExc_StandardError, 0}, |
Barry Warsaw | 412cdc2 | 1997-09-16 21:51:14 +0000 | [diff] [blame] | 1772 | {"ArithmeticError", &PyExc_ArithmeticError, 0}, |
Barry Warsaw | 757af0e | 1997-08-29 22:13:51 +0000 | [diff] [blame] | 1773 | {"LookupError", &PyExc_LookupError, 0}, |
| 1774 | {"AssertionError", &PyExc_AssertionError, 1}, |
| 1775 | {"AttributeError", &PyExc_AttributeError, 1}, |
| 1776 | {"EOFError", &PyExc_EOFError, 1}, |
| 1777 | {"FloatingPointError", &PyExc_FloatingPointError, 1}, |
| 1778 | {"IOError", &PyExc_IOError, 1}, |
| 1779 | {"ImportError", &PyExc_ImportError, 1}, |
| 1780 | {"IndexError", &PyExc_IndexError, 1}, |
| 1781 | {"KeyError", &PyExc_KeyError, 1}, |
| 1782 | {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 1}, |
| 1783 | {"MemoryError", &PyExc_MemoryError, 1}, |
| 1784 | {"NameError", &PyExc_NameError, 1}, |
| 1785 | {"OverflowError", &PyExc_OverflowError, 1}, |
| 1786 | {"RuntimeError", &PyExc_RuntimeError, 1}, |
| 1787 | {"SyntaxError", &PyExc_SyntaxError, 1}, |
| 1788 | {"SystemError", &PyExc_SystemError, 1}, |
| 1789 | {"SystemExit", &PyExc_SystemExit, 1}, |
| 1790 | {"TypeError", &PyExc_TypeError, 1}, |
| 1791 | {"ValueError", &PyExc_ValueError, 1}, |
| 1792 | {"ZeroDivisionError", &PyExc_ZeroDivisionError, 1}, |
| 1793 | {NULL, NULL} |
| 1794 | }; |
| 1795 | |
| 1796 | |
| 1797 | /* import exceptions module to extract class exceptions */ |
| 1798 | static void |
| 1799 | init_class_exc(dict) |
| 1800 | PyObject *dict; |
| 1801 | { |
| 1802 | int i; |
| 1803 | PyObject *m = PyImport_ImportModule("exceptions"); |
| 1804 | PyObject *d; |
| 1805 | PyObject *args; |
| 1806 | |
| 1807 | if (m == NULL || |
| 1808 | (d = PyModule_GetDict(m)) == NULL) |
| 1809 | { |
| 1810 | PyObject *f = PySys_GetObject("stderr"); |
| 1811 | if (Py_VerboseFlag) { |
| 1812 | PyFile_WriteString( |
| 1813 | "'import exceptions' failed; traceback:\n", f); |
| 1814 | PyErr_Print(); |
| 1815 | } |
| 1816 | else { |
| 1817 | PyFile_WriteString( |
| 1818 | "'import exceptions' failed; use -v for traceback\n", f); |
| 1819 | PyErr_Clear(); |
| 1820 | } |
| 1821 | PyFile_WriteString("defaulting to old style exceptions\n", f); |
| 1822 | return; |
| 1823 | } |
| 1824 | for (i = 0; bltin_exc[i].name; i++) { |
| 1825 | /* dig the exception out of the module */ |
| 1826 | PyObject *exc = PyDict_GetItemString(d, bltin_exc[i].name); |
| 1827 | if (!exc) |
| 1828 | Py_FatalError("built-in exception cannot be initialized"); |
| 1829 | |
| 1830 | Py_XDECREF(*bltin_exc[i].exc); |
| 1831 | |
| 1832 | /* squirrel away a pointer to the exception */ |
| 1833 | Py_INCREF(exc); |
| 1834 | *bltin_exc[i].exc = exc; |
| 1835 | |
| 1836 | /* and insert the name in the __builtin__ module */ |
| 1837 | PyDict_SetItemString(dict, bltin_exc[i].name, exc); |
| 1838 | } |
| 1839 | |
| 1840 | /* we need one pre-allocated instance */ |
| 1841 | args = Py_BuildValue("()"); |
| 1842 | if (args) { |
| 1843 | PyExc_MemoryErrorInst = |
| 1844 | PyEval_CallObject(PyExc_MemoryError, args); |
| 1845 | Py_DECREF(args); |
| 1846 | } |
| 1847 | |
| 1848 | /* we're done with the exceptions module */ |
| 1849 | Py_DECREF(m); |
| 1850 | |
| 1851 | if (PyErr_Occurred()) |
| 1852 | Py_FatalError("can't instantiate standard exceptions"); |
| 1853 | } |
| 1854 | |
| 1855 | |
| 1856 | static void |
| 1857 | fini_instances() |
| 1858 | { |
| 1859 | Py_XDECREF(PyExc_MemoryErrorInst); |
| 1860 | PyExc_MemoryErrorInst = NULL; |
| 1861 | } |
| 1862 | |
| 1863 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1864 | static PyObject * |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1865 | newstdexception(dict, name) |
| 1866 | PyObject *dict; |
Guido van Rossum | fb905c3 | 1991-12-16 15:42:38 +0000 | [diff] [blame] | 1867 | char *name; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1868 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1869 | PyObject *v = PyString_FromString(name); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1870 | if (v == NULL || PyDict_SetItemString(dict, name, v) != 0) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1871 | Py_FatalError("no mem for new standard exception"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1872 | return v; |
| 1873 | } |
| 1874 | |
| 1875 | static void |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1876 | initerrors(dict) |
| 1877 | PyObject *dict; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1878 | { |
Barry Warsaw | 757af0e | 1997-08-29 22:13:51 +0000 | [diff] [blame] | 1879 | int i; |
| 1880 | int exccnt = 0; |
| 1881 | for (i = 0; bltin_exc[i].name; i++, exccnt++) { |
| 1882 | if (bltin_exc[i].leaf_exc) |
| 1883 | *bltin_exc[i].exc = |
| 1884 | newstdexception(dict, bltin_exc[i].name); |
| 1885 | } |
| 1886 | |
| 1887 | /* This is kind of bogus because we special case the three new |
| 1888 | exceptions to be nearly forward compatible. But this means we |
| 1889 | hard code knowledge about exceptions.py into C here. I don't |
| 1890 | have a better solution, though |
| 1891 | */ |
| 1892 | PyExc_LookupError = PyTuple_New(2); |
| 1893 | Py_INCREF(PyExc_IndexError); |
| 1894 | PyTuple_SET_ITEM(PyExc_LookupError, 0, PyExc_IndexError); |
| 1895 | Py_INCREF(PyExc_KeyError); |
| 1896 | PyTuple_SET_ITEM(PyExc_LookupError, 1, PyExc_KeyError); |
| 1897 | PyDict_SetItemString(dict, "LookupError", PyExc_LookupError); |
| 1898 | |
Barry Warsaw | 412cdc2 | 1997-09-16 21:51:14 +0000 | [diff] [blame] | 1899 | PyExc_ArithmeticError = PyTuple_New(3); |
Barry Warsaw | 757af0e | 1997-08-29 22:13:51 +0000 | [diff] [blame] | 1900 | Py_INCREF(PyExc_OverflowError); |
Barry Warsaw | 412cdc2 | 1997-09-16 21:51:14 +0000 | [diff] [blame] | 1901 | PyTuple_SET_ITEM(PyExc_ArithmeticError, 0, PyExc_OverflowError); |
Barry Warsaw | 757af0e | 1997-08-29 22:13:51 +0000 | [diff] [blame] | 1902 | Py_INCREF(PyExc_ZeroDivisionError); |
Barry Warsaw | 412cdc2 | 1997-09-16 21:51:14 +0000 | [diff] [blame] | 1903 | PyTuple_SET_ITEM(PyExc_ArithmeticError, 1, PyExc_ZeroDivisionError); |
Barry Warsaw | 757af0e | 1997-08-29 22:13:51 +0000 | [diff] [blame] | 1904 | Py_INCREF(PyExc_FloatingPointError); |
Barry Warsaw | 412cdc2 | 1997-09-16 21:51:14 +0000 | [diff] [blame] | 1905 | PyTuple_SET_ITEM(PyExc_ArithmeticError, 2, PyExc_FloatingPointError); |
| 1906 | PyDict_SetItemString(dict, "ArithmeticError", PyExc_ArithmeticError); |
Barry Warsaw | 757af0e | 1997-08-29 22:13:51 +0000 | [diff] [blame] | 1907 | |
Barry Warsaw | b01a7fa | 1997-09-18 03:44:38 +0000 | [diff] [blame] | 1908 | PyExc_StandardError = PyTuple_New(exccnt-2); |
| 1909 | for (i = 2; bltin_exc[i].name; i++) { |
Barry Warsaw | 757af0e | 1997-08-29 22:13:51 +0000 | [diff] [blame] | 1910 | PyObject *exc = *bltin_exc[i].exc; |
| 1911 | Py_INCREF(exc); |
Barry Warsaw | b01a7fa | 1997-09-18 03:44:38 +0000 | [diff] [blame] | 1912 | PyTuple_SET_ITEM(PyExc_StandardError, i-2, exc); |
Barry Warsaw | 757af0e | 1997-08-29 22:13:51 +0000 | [diff] [blame] | 1913 | } |
| 1914 | PyDict_SetItemString(dict, "StandardError", PyExc_StandardError); |
Guido van Rossum | 0474832 | 1997-09-16 18:43:15 +0000 | [diff] [blame] | 1915 | |
| 1916 | /* Exception is treated differently; for now, it's == StandardError */ |
| 1917 | PyExc_Exception = PyExc_StandardError; |
| 1918 | Py_INCREF(PyExc_Exception); |
| 1919 | PyDict_SetItemString(dict, "Exception", PyExc_Exception); |
Barry Warsaw | 757af0e | 1997-08-29 22:13:51 +0000 | [diff] [blame] | 1920 | |
| 1921 | if (PyErr_Occurred()) |
| 1922 | Py_FatalError("Could not initialize built-in string exceptions"); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
| 1925 | static void |
| 1926 | finierrors() |
| 1927 | { |
Barry Warsaw | 757af0e | 1997-08-29 22:13:51 +0000 | [diff] [blame] | 1928 | int i; |
| 1929 | for (i = 0; bltin_exc[i].name; i++) { |
| 1930 | PyObject *exc = *bltin_exc[i].exc; |
| 1931 | Py_XDECREF(exc); |
| 1932 | *bltin_exc[i].exc = NULL; |
| 1933 | } |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | PyObject * |
Barry Warsaw | 757af0e | 1997-08-29 22:13:51 +0000 | [diff] [blame] | 1937 | _PyBuiltin_Init_1() |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1938 | { |
| 1939 | PyObject *mod, *dict; |
| 1940 | mod = Py_InitModule("__builtin__", builtin_methods); |
| 1941 | if (mod == NULL) |
| 1942 | return NULL; |
| 1943 | dict = PyModule_GetDict(mod); |
| 1944 | initerrors(dict); |
| 1945 | if (PyDict_SetItemString(dict, "None", Py_None) < 0) |
| 1946 | return NULL; |
| 1947 | if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0) |
| 1948 | return NULL; |
| 1949 | if (PyDict_SetItemString(dict, "__debug__", |
| 1950 | PyInt_FromLong(Py_OptimizeFlag == 0)) < 0) |
| 1951 | return NULL; |
Barry Warsaw | 757af0e | 1997-08-29 22:13:51 +0000 | [diff] [blame] | 1952 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1953 | return mod; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
| 1956 | void |
Barry Warsaw | 757af0e | 1997-08-29 22:13:51 +0000 | [diff] [blame] | 1957 | _PyBuiltin_Init_2(dict) |
| 1958 | PyObject *dict; |
| 1959 | { |
| 1960 | /* if Python was started with -X, initialize the class exceptions */ |
| 1961 | if (Py_UseClassExceptionsFlag) |
| 1962 | init_class_exc(dict); |
| 1963 | } |
| 1964 | |
| 1965 | |
| 1966 | void |
| 1967 | _PyBuiltin_Fini_1() |
| 1968 | { |
| 1969 | fini_instances(); |
| 1970 | } |
| 1971 | |
| 1972 | |
| 1973 | void |
| 1974 | _PyBuiltin_Fini_2() |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1975 | { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 1976 | finierrors(); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1977 | } |
Guido van Rossum | c6bb8f7 | 1991-07-01 18:42:41 +0000 | [diff] [blame] | 1978 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1979 | |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 1980 | /* Helper for filter(): filter a tuple through a function */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1981 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1982 | static PyObject * |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1983 | filtertuple(func, tuple) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1984 | PyObject *func; |
| 1985 | PyObject *tuple; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1986 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1987 | PyObject *result; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1988 | register int i, j; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1989 | int len = PyTuple_Size(tuple); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1990 | |
Guido van Rossum | b7b4562 | 1995-08-04 04:07:45 +0000 | [diff] [blame] | 1991 | if (len == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1992 | Py_INCREF(tuple); |
Guido van Rossum | b7b4562 | 1995-08-04 04:07:45 +0000 | [diff] [blame] | 1993 | return tuple; |
| 1994 | } |
| 1995 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1996 | if ((result = PyTuple_New(len)) == NULL) |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 1997 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1998 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1999 | for (i = j = 0; i < len; ++i) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2000 | PyObject *item, *good; |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2001 | int ok; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2002 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2003 | if ((item = PyTuple_GetItem(tuple, i)) == NULL) |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2004 | goto Fail_1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2005 | if (func == Py_None) { |
| 2006 | Py_INCREF(item); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2007 | good = item; |
| 2008 | } |
| 2009 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2010 | PyObject *arg = Py_BuildValue("(O)", item); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2011 | if (arg == NULL) |
| 2012 | goto Fail_1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2013 | good = PyEval_CallObject(func, arg); |
| 2014 | Py_DECREF(arg); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2015 | if (good == NULL) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2016 | goto Fail_1; |
| 2017 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2018 | ok = PyObject_IsTrue(good); |
| 2019 | Py_DECREF(good); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2020 | if (ok) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2021 | Py_INCREF(item); |
| 2022 | if (PyTuple_SetItem(result, j++, item) < 0) |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2023 | goto Fail_1; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2024 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2027 | if (_PyTuple_Resize(&result, j, 0) < 0) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2028 | return NULL; |
| 2029 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2030 | return result; |
| 2031 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2032 | Fail_1: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2033 | Py_DECREF(result); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2034 | return NULL; |
| 2035 | } |
| 2036 | |
| 2037 | |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 2038 | /* Helper for filter(): filter a string through a function */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2039 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2040 | static PyObject * |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2041 | filterstring(func, strobj) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2042 | PyObject *func; |
| 2043 | PyObject *strobj; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2044 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2045 | PyObject *result; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2046 | register int i, j; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2047 | int len = PyString_Size(strobj); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2048 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2049 | if (func == Py_None) { |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 2050 | /* No character is ever false -- share input string */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2051 | Py_INCREF(strobj); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 2052 | return strobj; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2053 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2054 | if ((result = PyString_FromStringAndSize(NULL, len)) == NULL) |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 2055 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2056 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2057 | for (i = j = 0; i < len; ++i) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2058 | PyObject *item, *arg, *good; |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2059 | int ok; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2060 | |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2061 | item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i); |
| 2062 | if (item == NULL) |
| 2063 | goto Fail_1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2064 | arg = Py_BuildValue("(O)", item); |
| 2065 | Py_DECREF(item); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2066 | if (arg == NULL) |
| 2067 | goto Fail_1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2068 | good = PyEval_CallObject(func, arg); |
| 2069 | Py_DECREF(arg); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2070 | if (good == NULL) |
| 2071 | goto Fail_1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2072 | ok = PyObject_IsTrue(good); |
| 2073 | Py_DECREF(good); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2074 | if (ok) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2075 | PyString_AS_STRING((PyStringObject *)result)[j++] = |
| 2076 | PyString_AS_STRING((PyStringObject *)item)[0]; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2077 | } |
| 2078 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2079 | if (j < len && _PyString_Resize(&result, j) < 0) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2080 | return NULL; |
| 2081 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2082 | return result; |
| 2083 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2084 | Fail_1: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2085 | Py_DECREF(result); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2086 | return NULL; |
| 2087 | } |
Guido van Rossum | c6472e9 | 1997-03-31 17:15:43 +0000 | [diff] [blame] | 2088 | |
| 2089 | /* Copied with modifications from stropmodule.c: atoi,atof.atol */ |
| 2090 | |
| 2091 | static PyObject * |
| 2092 | int_from_string(v) |
| 2093 | PyObject *v; |
| 2094 | { |
| 2095 | extern long PyOS_strtol Py_PROTO((const char *, char **, int)); |
| 2096 | char *s, *end; |
| 2097 | long x; |
| 2098 | char buffer[256]; /* For errors */ |
| 2099 | |
Guido van Rossum | 8f74571 | 1998-03-13 21:30:14 +0000 | [diff] [blame] | 2100 | s = PyString_AS_STRING(v); |
Guido van Rossum | c6472e9 | 1997-03-31 17:15:43 +0000 | [diff] [blame] | 2101 | while (*s && isspace(Py_CHARMASK(*s))) |
| 2102 | s++; |
| 2103 | if (s[0] == '\0') { |
| 2104 | PyErr_SetString(PyExc_ValueError, "empty string for int()"); |
| 2105 | return NULL; |
| 2106 | } |
| 2107 | errno = 0; |
| 2108 | x = PyOS_strtol(s, &end, 10); |
| 2109 | while (*end && isspace(Py_CHARMASK(*end))) |
| 2110 | end++; |
| 2111 | if (*end != '\0') { |
| 2112 | sprintf(buffer, "invalid literal for int(): %.200s", s); |
| 2113 | PyErr_SetString(PyExc_ValueError, buffer); |
| 2114 | return NULL; |
| 2115 | } |
Guido van Rossum | 8f74571 | 1998-03-13 21:30:14 +0000 | [diff] [blame] | 2116 | else if (end-s != PyString_GET_SIZE(v)) { |
| 2117 | PyErr_SetString(PyExc_ValueError, |
| 2118 | "null byte in argument for int()"); |
| 2119 | return NULL; |
| 2120 | } |
Guido van Rossum | c6472e9 | 1997-03-31 17:15:43 +0000 | [diff] [blame] | 2121 | else if (errno != 0) { |
| 2122 | sprintf(buffer, "int() literal too large: %.200s", s); |
| 2123 | PyErr_SetString(PyExc_ValueError, buffer); |
| 2124 | return NULL; |
| 2125 | } |
| 2126 | return PyInt_FromLong(x); |
| 2127 | } |
| 2128 | |
| 2129 | static PyObject * |
| 2130 | long_from_string(v) |
| 2131 | PyObject *v; |
| 2132 | { |
| 2133 | char *s, *end; |
| 2134 | PyObject *x; |
| 2135 | char buffer[256]; /* For errors */ |
| 2136 | |
Guido van Rossum | 8f74571 | 1998-03-13 21:30:14 +0000 | [diff] [blame] | 2137 | s = PyString_AS_STRING(v); |
Guido van Rossum | c6472e9 | 1997-03-31 17:15:43 +0000 | [diff] [blame] | 2138 | while (*s && isspace(Py_CHARMASK(*s))) |
| 2139 | s++; |
| 2140 | if (s[0] == '\0') { |
| 2141 | PyErr_SetString(PyExc_ValueError, "empty string for long()"); |
| 2142 | return NULL; |
| 2143 | } |
| 2144 | x = PyLong_FromString(s, &end, 10); |
| 2145 | if (x == NULL) |
| 2146 | return NULL; |
| 2147 | while (*end && isspace(Py_CHARMASK(*end))) |
| 2148 | end++; |
| 2149 | if (*end != '\0') { |
| 2150 | sprintf(buffer, "invalid literal for long(): %.200s", s); |
| 2151 | PyErr_SetString(PyExc_ValueError, buffer); |
| 2152 | Py_DECREF(x); |
| 2153 | return NULL; |
| 2154 | } |
Guido van Rossum | 8f74571 | 1998-03-13 21:30:14 +0000 | [diff] [blame] | 2155 | else if (end-s != PyString_GET_SIZE(v)) { |
| 2156 | PyErr_SetString(PyExc_ValueError, |
| 2157 | "null byte in argument for float()"); |
| 2158 | return NULL; |
| 2159 | } |
Guido van Rossum | c6472e9 | 1997-03-31 17:15:43 +0000 | [diff] [blame] | 2160 | return x; |
| 2161 | } |
| 2162 | |
| 2163 | static PyObject * |
| 2164 | float_from_string(v) |
| 2165 | PyObject *v; |
| 2166 | { |
| 2167 | extern double strtod Py_PROTO((const char *, char **)); |
| 2168 | char *s, *end; |
| 2169 | double x; |
| 2170 | char buffer[256]; /* For errors */ |
| 2171 | |
Guido van Rossum | 8f74571 | 1998-03-13 21:30:14 +0000 | [diff] [blame] | 2172 | s = PyString_AS_STRING(v); |
Guido van Rossum | c6472e9 | 1997-03-31 17:15:43 +0000 | [diff] [blame] | 2173 | while (*s && isspace(Py_CHARMASK(*s))) |
| 2174 | s++; |
| 2175 | if (s[0] == '\0') { |
| 2176 | PyErr_SetString(PyExc_ValueError, "empty string for float()"); |
| 2177 | return NULL; |
| 2178 | } |
| 2179 | errno = 0; |
| 2180 | PyFPE_START_PROTECT("float_from_string", return 0) |
| 2181 | x = strtod(s, &end); |
| 2182 | PyFPE_END_PROTECT(x) |
| 2183 | while (*end && isspace(Py_CHARMASK(*end))) |
| 2184 | end++; |
| 2185 | if (*end != '\0') { |
| 2186 | sprintf(buffer, "invalid literal for float(): %.200s", s); |
| 2187 | PyErr_SetString(PyExc_ValueError, buffer); |
| 2188 | return NULL; |
| 2189 | } |
Guido van Rossum | 8f74571 | 1998-03-13 21:30:14 +0000 | [diff] [blame] | 2190 | else if (end-s != PyString_GET_SIZE(v)) { |
| 2191 | PyErr_SetString(PyExc_ValueError, |
| 2192 | "null byte in argument for float()"); |
| 2193 | return NULL; |
| 2194 | } |
Guido van Rossum | c6472e9 | 1997-03-31 17:15:43 +0000 | [diff] [blame] | 2195 | else if (errno != 0) { |
| 2196 | sprintf(buffer, "float() literal too large: %.200s", s); |
| 2197 | PyErr_SetString(PyExc_ValueError, buffer); |
| 2198 | return NULL; |
| 2199 | } |
| 2200 | return PyFloat_FromDouble(x); |
| 2201 | } |