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