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