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