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