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 | |
| 34 | #include "allobjects.h" |
| 35 | |
| 36 | #include "node.h" |
| 37 | #include "graminit.h" |
Guido van Rossum | 86cd6e6 | 1991-01-21 15:12:35 +0000 | [diff] [blame] | 38 | #include "bltinmodule.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 39 | #include "import.h" |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 40 | #include "compile.h" |
| 41 | #include "eval.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | fe4b6ee | 1996-08-08 18:49:41 +0000 | [diff] [blame] | 43 | #include "mymath.h" |
| 44 | |
Guido van Rossum | 1a2c5cb | 1996-12-10 15:37:36 +0000 | [diff] [blame] | 45 | #ifdef HAVE_UNISTD_H |
| 46 | #include <unistd.h> |
| 47 | #endif |
| 48 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 49 | /* Forward */ |
| 50 | static object *filterstring PROTO((object *, object *)); |
| 51 | static object *filtertuple PROTO((object *, object *)); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 53 | static object * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 54 | builtin___import__(self, args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 55 | object *self; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 56 | object *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 57 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 58 | char *name; |
Guido van Rossum | 24c1374 | 1995-02-14 09:42:43 +0000 | [diff] [blame] | 59 | object *globals = NULL; |
| 60 | object *locals = NULL; |
| 61 | object *fromlist = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 62 | |
Guido van Rossum | 24c1374 | 1995-02-14 09:42:43 +0000 | [diff] [blame] | 63 | if (!newgetargs(args, "s|OOO:__import__", |
| 64 | &name, &globals, &locals, &fromlist)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 65 | return NULL; |
Guido van Rossum | 7f9fa97 | 1995-01-20 16:53:12 +0000 | [diff] [blame] | 66 | return import_module(name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | |
| 70 | static object * |
| 71 | builtin_abs(self, args) |
| 72 | object *self; |
| 73 | object *args; |
| 74 | { |
| 75 | object *v; |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 76 | number_methods *nm; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 77 | |
| 78 | if (!newgetargs(args, "O:abs", &v)) |
| 79 | return NULL; |
| 80 | if ((nm = v->ob_type->tp_as_number) == NULL) { |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 81 | err_setstr(TypeError, "abs() requires numeric argument"); |
| 82 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 83 | } |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 84 | return (*nm->nb_absolute)(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static object * |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 88 | builtin_apply(self, args) |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 89 | object *self; |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 90 | object *args; |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 91 | { |
Guido van Rossum | c96ef6a | 1996-01-26 20:44:30 +0000 | [diff] [blame] | 92 | object *func, *alist = NULL, *kwdict = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 93 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 94 | if (!newgetargs(args, "O|OO:apply", &func, &alist, &kwdict)) |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 95 | return NULL; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 96 | if (alist != NULL && !is_tupleobject(alist)) { |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 97 | err_setstr(TypeError, "apply() 2nd argument must be tuple"); |
| 98 | return NULL; |
| 99 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 100 | if (kwdict != NULL && !is_dictobject(kwdict)) { |
| 101 | err_setstr(TypeError, |
| 102 | "apply() 3rd argument must be dictionary"); |
| 103 | return NULL; |
| 104 | } |
| 105 | return PyEval_CallObjectWithKeywords(func, alist, kwdict); |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 108 | static object * |
| 109 | builtin_callable(self, args) |
| 110 | object *self; |
| 111 | object *args; |
| 112 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 113 | object *v; |
| 114 | |
| 115 | if (!newgetargs(args, "O:callable", &v)) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 116 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 117 | return newintobject((long)callable(v)); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 120 | static object * |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 121 | builtin_filter(self, args) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 122 | object *self; |
| 123 | object *args; |
| 124 | { |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 125 | object *func, *seq, *result; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 126 | sequence_methods *sqf; |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 127 | int len; |
| 128 | register int i, j; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 129 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 130 | if (!newgetargs(args, "OO:filter", &func, &seq)) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 131 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 132 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 133 | if (is_stringobject(seq)) { |
| 134 | object *r = filterstring(func, seq); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 135 | return r; |
| 136 | } |
| 137 | |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 138 | if (is_tupleobject(seq)) { |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 139 | object *r = filtertuple(func, seq); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 140 | return r; |
| 141 | } |
| 142 | |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 143 | if ((sqf = seq->ob_type->tp_as_sequence) == NULL) { |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 144 | err_setstr(TypeError, |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 145 | "argument 2 to filter() must be a sequence type"); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 146 | goto Fail_2; |
| 147 | } |
| 148 | |
| 149 | if ((len = (*sqf->sq_length)(seq)) < 0) |
| 150 | goto Fail_2; |
| 151 | |
| 152 | if (is_listobject(seq) && seq->ob_refcnt == 1) { |
| 153 | INCREF(seq); |
| 154 | result = seq; |
| 155 | } |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 156 | else { |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 157 | if ((result = newlistobject(len)) == NULL) |
| 158 | goto Fail_2; |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 159 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 160 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 161 | for (i = j = 0; ; ++i) { |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 162 | object *item, *good; |
| 163 | int ok; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 164 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 165 | if ((item = (*sqf->sq_item)(seq, i)) == NULL) { |
| 166 | if (i < len) |
| 167 | goto Fail_1; |
| 168 | if (err_occurred() == IndexError) { |
| 169 | err_clear(); |
| 170 | break; |
| 171 | } |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 172 | goto Fail_1; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 173 | } |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 174 | |
| 175 | if (func == None) { |
| 176 | good = item; |
Guido van Rossum | 58b6873 | 1995-01-10 17:40:55 +0000 | [diff] [blame] | 177 | INCREF(good); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 178 | } |
| 179 | else { |
| 180 | object *arg = mkvalue("(O)", item); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 181 | if (arg == NULL) |
| 182 | goto Fail_1; |
| 183 | good = call_object(func, arg); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 184 | DECREF(arg); |
Guido van Rossum | 58b6873 | 1995-01-10 17:40:55 +0000 | [diff] [blame] | 185 | if (good == NULL) { |
| 186 | DECREF(item); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 187 | goto Fail_1; |
Guido van Rossum | 58b6873 | 1995-01-10 17:40:55 +0000 | [diff] [blame] | 188 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 189 | } |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 190 | ok = testbool(good); |
| 191 | DECREF(good); |
| 192 | if (ok) { |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 193 | if (j < len) { |
| 194 | if (setlistitem(result, j++, item) < 0) |
| 195 | goto Fail_1; |
| 196 | } |
| 197 | else { |
| 198 | j++; |
| 199 | if (addlistitem(result, item) < 0) |
| 200 | goto Fail_1; |
| 201 | } |
Guido van Rossum | 58b6873 | 1995-01-10 17:40:55 +0000 | [diff] [blame] | 202 | } else { |
| 203 | DECREF(item); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 204 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 207 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 208 | if (j < len && setlistslice(result, j, len, NULL) < 0) |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 209 | goto Fail_1; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 210 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 211 | return result; |
| 212 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 213 | Fail_1: |
| 214 | DECREF(result); |
| 215 | Fail_2: |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 216 | return NULL; |
| 217 | } |
| 218 | |
| 219 | static object * |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 220 | builtin_chr(self, args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 221 | object *self; |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 222 | object *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 223 | { |
| 224 | long x; |
| 225 | char s[1]; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 226 | |
| 227 | if (!newgetargs(args, "l:chr", &x)) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 228 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 229 | if (x < 0 || x >= 256) { |
Guido van Rossum | 50afb7a | 1991-12-10 13:52:31 +0000 | [diff] [blame] | 230 | err_setstr(ValueError, "chr() arg not in range(256)"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 231 | return NULL; |
| 232 | } |
| 233 | s[0] = x; |
| 234 | return newsizedstringobject(s, 1); |
| 235 | } |
| 236 | |
| 237 | static object * |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 238 | builtin_cmp(self, args) |
| 239 | object *self; |
| 240 | object *args; |
| 241 | { |
| 242 | object *a, *b; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 243 | |
| 244 | if (!newgetargs(args, "OO:cmp", &a, &b)) |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 245 | return NULL; |
| 246 | return newintobject((long)cmpobject(a, b)); |
| 247 | } |
| 248 | |
| 249 | static object * |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 250 | builtin_coerce(self, args) |
| 251 | object *self; |
| 252 | object *args; |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 253 | { |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 254 | object *v, *w; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 255 | object *res; |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 256 | |
| 257 | if (!newgetargs(args, "OO:coerce", &v, &w)) |
| 258 | return NULL; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 259 | if (coerce(&v, &w) < 0) |
| 260 | return NULL; |
| 261 | res = mkvalue("(OO)", v, w); |
| 262 | DECREF(v); |
| 263 | DECREF(w); |
| 264 | return res; |
| 265 | } |
| 266 | |
| 267 | static object * |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 268 | builtin_compile(self, args) |
| 269 | object *self; |
| 270 | object *args; |
| 271 | { |
| 272 | char *str; |
| 273 | char *filename; |
| 274 | char *startstr; |
| 275 | int start; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 276 | |
| 277 | if (!newgetargs(args, "sss:compile", &str, &filename, &startstr)) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 278 | return NULL; |
| 279 | if (strcmp(startstr, "exec") == 0) |
| 280 | start = file_input; |
| 281 | else if (strcmp(startstr, "eval") == 0) |
| 282 | start = eval_input; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 283 | else if (strcmp(startstr, "single") == 0) |
| 284 | start = single_input; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 285 | else { |
| 286 | err_setstr(ValueError, |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 287 | "compile() mode must be 'exec' or 'eval' or 'single'"); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 288 | return NULL; |
| 289 | } |
| 290 | return compile_string(str, filename, start); |
| 291 | } |
| 292 | |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 293 | #ifndef WITHOUT_COMPLEX |
| 294 | |
| 295 | static object * |
| 296 | builtin_complex(self, args) |
| 297 | object *self; |
| 298 | object *args; |
| 299 | { |
Guido van Rossum | fe4b6ee | 1996-08-08 18:49:41 +0000 | [diff] [blame] | 300 | object *r, *i, *tmp; |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 301 | number_methods *nbr, *nbi; |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 302 | Py_complex cr, ci; |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 303 | |
| 304 | i = NULL; |
| 305 | if (!newgetargs(args, "O|O:complex", &r, &i)) |
| 306 | return NULL; |
| 307 | if ((nbr = r->ob_type->tp_as_number) == NULL || |
| 308 | nbr->nb_float == NULL || (i != NULL && |
| 309 | ((nbi = i->ob_type->tp_as_number) == NULL || |
| 310 | nbi->nb_float == NULL))) { |
| 311 | err_setstr(TypeError, |
| 312 | "complex() argument can't be converted to complex"); |
| 313 | return NULL; |
| 314 | } |
Guido van Rossum | ed0af8f | 1996-12-05 23:18:18 +0000 | [diff] [blame] | 315 | /* XXX Hack to support classes with __complex__ method */ |
| 316 | if (is_instanceobject(r)) { |
| 317 | static object *complexstr; |
| 318 | object *f; |
| 319 | if (complexstr == NULL) { |
Guido van Rossum | 8d75161 | 1997-01-18 08:04:16 +0000 | [diff] [blame] | 320 | complexstr = PyString_InternFromString("__complex__"); |
Guido van Rossum | ed0af8f | 1996-12-05 23:18:18 +0000 | [diff] [blame] | 321 | if (complexstr == NULL) |
| 322 | return NULL; |
| 323 | } |
| 324 | f = getattro(r, complexstr); |
| 325 | if (f == NULL) |
| 326 | err_clear(); |
| 327 | else { |
| 328 | object *args = mkvalue("()"); |
| 329 | if (args == NULL) |
| 330 | return NULL; |
| 331 | r = call_object(f, args); |
| 332 | DECREF(args); |
| 333 | if (r == NULL) |
| 334 | return NULL; |
| 335 | } |
| 336 | } |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 337 | if (is_complexobject(r)) |
| 338 | cr = ((complexobject*)r)->cval; |
| 339 | else { |
Guido van Rossum | fe4b6ee | 1996-08-08 18:49:41 +0000 | [diff] [blame] | 340 | tmp = (*nbr->nb_float)(r); |
| 341 | if (tmp == NULL) |
| 342 | return NULL; |
| 343 | cr.real = getfloatvalue(tmp); |
| 344 | DECREF(tmp); |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 345 | cr.imag = 0.; |
| 346 | } |
| 347 | if (i == NULL) { |
| 348 | ci.real = 0.; |
| 349 | ci.imag = 0.; |
| 350 | } |
| 351 | else if (is_complexobject(i)) |
| 352 | ci = ((complexobject*)i)->cval; |
| 353 | else { |
Guido van Rossum | b072150 | 1996-09-07 15:55:27 +0000 | [diff] [blame] | 354 | tmp = (*nbr->nb_float)(i); |
Guido van Rossum | fe4b6ee | 1996-08-08 18:49:41 +0000 | [diff] [blame] | 355 | if (tmp == NULL) |
| 356 | return NULL; |
| 357 | ci.real = getfloatvalue(tmp); |
| 358 | DECREF(tmp); |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 359 | ci.imag = 0.; |
| 360 | } |
| 361 | cr.real -= ci.imag; |
| 362 | cr.imag += ci.real; |
| 363 | return newcomplexobject(cr); |
| 364 | } |
| 365 | |
| 366 | #endif |
| 367 | |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 368 | static object * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 369 | builtin_dir(self, args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 370 | object *self; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 371 | object *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 372 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 373 | object *v = NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 374 | object *d; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 375 | |
| 376 | if (!newgetargs(args, "|O:dir", &v)) |
| 377 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 378 | if (v == NULL) { |
| 379 | d = getlocals(); |
Guido van Rossum | dc8a108 | 1991-10-20 20:11:03 +0000 | [diff] [blame] | 380 | INCREF(d); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 381 | } |
| 382 | else { |
Guido van Rossum | dc8a108 | 1991-10-20 20:11:03 +0000 | [diff] [blame] | 383 | d = getattr(v, "__dict__"); |
| 384 | if (d == NULL) { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 385 | err_setstr(TypeError, |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 386 | "dir() argument must have __dict__ attribute"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 387 | return NULL; |
| 388 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 389 | } |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 390 | if (is_dictobject(d)) { |
Guido van Rossum | dc8a108 | 1991-10-20 20:11:03 +0000 | [diff] [blame] | 391 | v = getdictkeys(d); |
| 392 | if (sortlist(v) != 0) { |
| 393 | DECREF(v); |
| 394 | v = NULL; |
| 395 | } |
| 396 | } |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 397 | else { |
Guido van Rossum | 795ba58 | 1996-05-23 22:49:07 +0000 | [diff] [blame] | 398 | v = PyObject_CallMethod(d, "keys", NULL); |
| 399 | if (v == NULL) { |
| 400 | PyErr_Clear(); |
| 401 | v = newlistobject(0); |
| 402 | } |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 403 | } |
Guido van Rossum | dc8a108 | 1991-10-20 20:11:03 +0000 | [diff] [blame] | 404 | DECREF(d); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 405 | return v; |
| 406 | } |
| 407 | |
| 408 | static object * |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 409 | do_divmod(v, w) |
| 410 | object *v, *w; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 411 | { |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 412 | object *res; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 413 | |
Guido van Rossum | 180d7b4 | 1994-09-29 09:45:57 +0000 | [diff] [blame] | 414 | if (is_instanceobject(v) || is_instanceobject(w)) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 415 | return instancebinop(v, w, "__divmod__", "__rdivmod__", |
| 416 | do_divmod); |
Guido van Rossum | c6bb8f7 | 1991-07-01 18:42:41 +0000 | [diff] [blame] | 417 | if (v->ob_type->tp_as_number == NULL || |
| 418 | w->ob_type->tp_as_number == NULL) { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 419 | err_setstr(TypeError, |
| 420 | "divmod() requires numeric or class instance arguments"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 421 | return NULL; |
| 422 | } |
Guido van Rossum | c6bb8f7 | 1991-07-01 18:42:41 +0000 | [diff] [blame] | 423 | if (coerce(&v, &w) != 0) |
| 424 | return NULL; |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 425 | res = (*v->ob_type->tp_as_number->nb_divmod)(v, w); |
Guido van Rossum | c6bb8f7 | 1991-07-01 18:42:41 +0000 | [diff] [blame] | 426 | DECREF(v); |
| 427 | DECREF(w); |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 428 | return res; |
| 429 | } |
| 430 | |
| 431 | static object * |
| 432 | builtin_divmod(self, args) |
| 433 | object *self; |
| 434 | object *args; |
| 435 | { |
| 436 | object *v, *w; |
| 437 | |
| 438 | if (!newgetargs(args, "OO:divmod", &v, &w)) |
| 439 | return NULL; |
| 440 | return do_divmod(v, w); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | static object * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 444 | builtin_eval(self, args) |
| 445 | object *self; |
| 446 | object *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 447 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 448 | object *cmd; |
Guido van Rossum | 84eaa83 | 1995-01-10 10:47:05 +0000 | [diff] [blame] | 449 | object *globals = None, *locals = None; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 450 | char *str; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 451 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 452 | if (!newgetargs(args, "O|O!O!:eval", |
| 453 | &cmd, |
| 454 | &Mappingtype, &globals, |
| 455 | &Mappingtype, &locals)) |
| 456 | return NULL; |
Guido van Rossum | 84eaa83 | 1995-01-10 10:47:05 +0000 | [diff] [blame] | 457 | if (globals == None) { |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 458 | globals = getglobals(); |
Guido van Rossum | 84eaa83 | 1995-01-10 10:47:05 +0000 | [diff] [blame] | 459 | if (locals == None) |
| 460 | locals = getlocals(); |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 461 | } |
Guido van Rossum | 84eaa83 | 1995-01-10 10:47:05 +0000 | [diff] [blame] | 462 | else if (locals == None) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 463 | locals = globals; |
| 464 | if (dictlookup(globals, "__builtins__") == NULL) { |
| 465 | if (dictinsert(globals, "__builtins__", getbuiltins()) != 0) |
| 466 | return NULL; |
| 467 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 468 | if (is_codeobject(cmd)) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 469 | return eval_code((codeobject *) cmd, globals, locals); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 470 | if (!is_stringobject(cmd)) { |
| 471 | err_setstr(TypeError, |
| 472 | "eval() argument 1 must be string or code object"); |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 473 | return NULL; |
| 474 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 475 | str = getstringvalue(cmd); |
| 476 | if (strlen(str) != getstringsize(cmd)) { |
| 477 | err_setstr(ValueError, |
| 478 | "embedded '\\0' in string arg"); |
| 479 | return NULL; |
Guido van Rossum | f08ab0a | 1992-03-04 16:41:41 +0000 | [diff] [blame] | 480 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 481 | while (*str == ' ' || *str == '\t') |
| 482 | str++; |
| 483 | return run_string(str, eval_input, globals, locals); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | static object * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 487 | builtin_execfile(self, args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 488 | object *self; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 489 | object *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 490 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 491 | char *filename; |
Guido van Rossum | 84eaa83 | 1995-01-10 10:47:05 +0000 | [diff] [blame] | 492 | object *globals = None, *locals = None; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 493 | object *res; |
Guido van Rossum | 0f61f8a | 1992-02-25 18:55:05 +0000 | [diff] [blame] | 494 | FILE* fp; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 495 | |
| 496 | if (!newgetargs(args, "s|O!O!:execfile", |
| 497 | &filename, |
| 498 | &Mappingtype, &globals, |
| 499 | &Mappingtype, &locals)) |
Guido van Rossum | 0f61f8a | 1992-02-25 18:55:05 +0000 | [diff] [blame] | 500 | return NULL; |
Guido van Rossum | 84eaa83 | 1995-01-10 10:47:05 +0000 | [diff] [blame] | 501 | if (globals == None) { |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 502 | globals = getglobals(); |
Guido van Rossum | 84eaa83 | 1995-01-10 10:47:05 +0000 | [diff] [blame] | 503 | if (locals == None) |
| 504 | locals = getlocals(); |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 505 | } |
Guido van Rossum | 84eaa83 | 1995-01-10 10:47:05 +0000 | [diff] [blame] | 506 | else if (locals == None) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 507 | locals = globals; |
| 508 | if (dictlookup(globals, "__builtins__") == NULL) { |
| 509 | if (dictinsert(globals, "__builtins__", getbuiltins()) != 0) |
| 510 | return NULL; |
| 511 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 512 | BGN_SAVE |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 513 | fp = fopen(filename, "r"); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 514 | END_SAVE |
Guido van Rossum | 0f61f8a | 1992-02-25 18:55:05 +0000 | [diff] [blame] | 515 | if (fp == NULL) { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 516 | err_errno(IOError); |
Guido van Rossum | 0f61f8a | 1992-02-25 18:55:05 +0000 | [diff] [blame] | 517 | return NULL; |
| 518 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 519 | res = run_file(fp, filename, file_input, globals, locals); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 520 | BGN_SAVE |
Guido van Rossum | 0f61f8a | 1992-02-25 18:55:05 +0000 | [diff] [blame] | 521 | fclose(fp); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 522 | END_SAVE |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 523 | return res; |
Guido van Rossum | 0f61f8a | 1992-02-25 18:55:05 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | static object * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 527 | builtin_float(self, args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 528 | object *self; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 529 | object *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 530 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 531 | object *v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 532 | number_methods *nb; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 533 | |
| 534 | if (!newgetargs(args, "O:float", &v)) |
| 535 | return NULL; |
| 536 | if ((nb = v->ob_type->tp_as_number) == NULL || |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 537 | nb->nb_float == NULL) { |
| 538 | err_setstr(TypeError, |
| 539 | "float() argument can't be converted to float"); |
| 540 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 541 | } |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 542 | return (*nb->nb_float)(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | static object * |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 546 | builtin_getattr(self, args) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 547 | object *self; |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 548 | object *args; |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 549 | { |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 550 | object *v; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 551 | object *name; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 552 | |
| 553 | if (!newgetargs(args, "OS:getattr", &v, &name)) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 554 | return NULL; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 555 | return getattro(v, name); |
| 556 | } |
| 557 | |
| 558 | static object * |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 559 | builtin_globals(self, args) |
| 560 | object *self; |
| 561 | object *args; |
| 562 | { |
| 563 | object *d; |
| 564 | |
| 565 | if (!newgetargs(args, "")) |
| 566 | return NULL; |
| 567 | d = getglobals(); |
| 568 | INCREF(d); |
| 569 | return d; |
| 570 | } |
| 571 | |
| 572 | static object * |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 573 | builtin_hasattr(self, args) |
| 574 | object *self; |
| 575 | object *args; |
| 576 | { |
| 577 | object *v; |
| 578 | object *name; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 579 | |
| 580 | if (!newgetargs(args, "OS:hasattr", &v, &name)) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 581 | return NULL; |
| 582 | v = getattro(v, name); |
| 583 | if (v == NULL) { |
| 584 | err_clear(); |
| 585 | return newintobject(0L); |
| 586 | } |
| 587 | DECREF(v); |
| 588 | return newintobject(1L); |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | static object * |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 592 | builtin_id(self, args) |
| 593 | object *self; |
| 594 | object *args; |
| 595 | { |
| 596 | object *v; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 597 | |
| 598 | if (!newgetargs(args, "O:id", &v)) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 599 | return NULL; |
| 600 | return newintobject((long)v); |
| 601 | } |
| 602 | |
| 603 | static object * |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 604 | builtin_map(self, args) |
| 605 | object *self; |
| 606 | object *args; |
| 607 | { |
| 608 | typedef struct { |
| 609 | object *seq; |
| 610 | sequence_methods *sqf; |
| 611 | int len; |
| 612 | } sequence; |
| 613 | |
| 614 | object *func, *result; |
| 615 | sequence *seqs = NULL, *sqp; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 616 | int n, len; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 617 | register int i, j; |
| 618 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 619 | n = gettuplesize(args); |
| 620 | if (n < 2) { |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 621 | err_setstr(TypeError, "map() requires at least two args"); |
| 622 | return NULL; |
| 623 | } |
| 624 | |
| 625 | func = gettupleitem(args, 0); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 626 | n--; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 627 | |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 628 | if ((seqs = NEW(sequence, n)) == NULL) { |
| 629 | err_nomem(); |
| 630 | goto Fail_2; |
| 631 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 632 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 633 | for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) { |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 634 | int curlen; |
| 635 | |
| 636 | if ((sqp->seq = gettupleitem(args, i + 1)) == NULL) |
| 637 | goto Fail_2; |
| 638 | |
| 639 | if (! (sqp->sqf = sqp->seq->ob_type->tp_as_sequence)) { |
| 640 | static char errmsg[] = |
| 641 | "argument %d to map() must be a sequence object"; |
| 642 | char errbuf[sizeof(errmsg) + 3]; |
| 643 | |
| 644 | sprintf(errbuf, errmsg, i+2); |
| 645 | err_setstr(TypeError, errbuf); |
| 646 | goto Fail_2; |
| 647 | } |
| 648 | |
| 649 | if ((curlen = sqp->len = (*sqp->sqf->sq_length)(sqp->seq)) < 0) |
| 650 | goto Fail_2; |
| 651 | |
| 652 | if (curlen > len) |
| 653 | len = curlen; |
| 654 | } |
| 655 | |
| 656 | if ((result = (object *) newlistobject(len)) == NULL) |
| 657 | goto Fail_2; |
| 658 | |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 659 | /* XXX Special case map(None, single_list) could be more efficient */ |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 660 | for (i = 0; ; ++i) { |
Guido van Rossum | ed0af8f | 1996-12-05 23:18:18 +0000 | [diff] [blame] | 661 | object *alist, *item=NULL, *value; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 662 | int any = 0; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 663 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 664 | if (func == None && n == 1) |
Guido van Rossum | 3212031 | 1995-07-10 13:52:21 +0000 | [diff] [blame] | 665 | alist = NULL; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 666 | else { |
Guido van Rossum | 3212031 | 1995-07-10 13:52:21 +0000 | [diff] [blame] | 667 | if ((alist = newtupleobject(n)) == NULL) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 668 | goto Fail_1; |
| 669 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 670 | |
| 671 | for (j = 0, sqp = seqs; j < n; ++j, ++sqp) { |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 672 | if (sqp->len < 0) { |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 673 | INCREF(None); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 674 | item = None; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 675 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 676 | else { |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 677 | item = (*sqp->sqf->sq_item)(sqp->seq, i); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 678 | if (item == NULL) { |
| 679 | if (i < sqp->len) |
| 680 | goto Fail_0; |
| 681 | if (err_occurred() == IndexError) { |
| 682 | err_clear(); |
| 683 | INCREF(None); |
| 684 | item = None; |
| 685 | sqp->len = -1; |
| 686 | } |
| 687 | else { |
| 688 | goto Fail_0; |
| 689 | } |
| 690 | } |
| 691 | else |
| 692 | any = 1; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 693 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 694 | } |
Guido van Rossum | 3212031 | 1995-07-10 13:52:21 +0000 | [diff] [blame] | 695 | if (!alist) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 696 | break; |
Guido van Rossum | 3212031 | 1995-07-10 13:52:21 +0000 | [diff] [blame] | 697 | if (settupleitem(alist, j, item) < 0) { |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 698 | DECREF(item); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 699 | goto Fail_0; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 700 | } |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 701 | continue; |
| 702 | |
| 703 | Fail_0: |
Guido van Rossum | 3212031 | 1995-07-10 13:52:21 +0000 | [diff] [blame] | 704 | XDECREF(alist); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 705 | goto Fail_1; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Guido van Rossum | 3212031 | 1995-07-10 13:52:21 +0000 | [diff] [blame] | 708 | if (!alist) |
| 709 | alist = item; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 710 | |
| 711 | if (!any) { |
Guido van Rossum | 3212031 | 1995-07-10 13:52:21 +0000 | [diff] [blame] | 712 | DECREF(alist); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 713 | break; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 714 | } |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 715 | |
| 716 | if (func == None) |
Guido van Rossum | 3212031 | 1995-07-10 13:52:21 +0000 | [diff] [blame] | 717 | value = alist; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 718 | else { |
Guido van Rossum | 3212031 | 1995-07-10 13:52:21 +0000 | [diff] [blame] | 719 | value = call_object(func, alist); |
| 720 | DECREF(alist); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 721 | if (value == NULL) |
| 722 | goto Fail_1; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 723 | } |
| 724 | if (i >= len) { |
| 725 | if (addlistitem(result, value) < 0) |
| 726 | goto Fail_1; |
| 727 | } |
| 728 | else { |
| 729 | if (setlistitem(result, i, value) < 0) |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 730 | goto Fail_1; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 734 | DEL(seqs); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 735 | return result; |
| 736 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 737 | Fail_1: |
| 738 | DECREF(result); |
| 739 | Fail_2: |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 740 | if (seqs) DEL(seqs); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 741 | return NULL; |
| 742 | } |
| 743 | |
| 744 | static object * |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 745 | builtin_setattr(self, args) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 746 | object *self; |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 747 | object *args; |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 748 | { |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 749 | object *v; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 750 | object *name; |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 751 | object *value; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 752 | |
| 753 | if (!newgetargs(args, "OSO:setattr", &v, &name, &value)) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 754 | return NULL; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 755 | if (setattro(v, name, value) != 0) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 756 | return NULL; |
| 757 | INCREF(None); |
| 758 | return None; |
| 759 | } |
| 760 | |
| 761 | static object * |
Guido van Rossum | 14144fc | 1994-08-29 12:53:40 +0000 | [diff] [blame] | 762 | builtin_delattr(self, args) |
| 763 | object *self; |
| 764 | object *args; |
| 765 | { |
| 766 | object *v; |
| 767 | object *name; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 768 | |
| 769 | if (!newgetargs(args, "OS:delattr", &v, &name)) |
Guido van Rossum | 14144fc | 1994-08-29 12:53:40 +0000 | [diff] [blame] | 770 | return NULL; |
| 771 | if (setattro(v, name, (object *)NULL) != 0) |
| 772 | return NULL; |
| 773 | INCREF(None); |
| 774 | return None; |
| 775 | } |
| 776 | |
| 777 | static object * |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 778 | builtin_hash(self, args) |
| 779 | object *self; |
| 780 | object *args; |
| 781 | { |
| 782 | object *v; |
| 783 | long x; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 784 | |
| 785 | if (!newgetargs(args, "O:hash", &v)) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 786 | return NULL; |
| 787 | x = hashobject(v); |
| 788 | if (x == -1) |
| 789 | return NULL; |
| 790 | return newintobject(x); |
| 791 | } |
| 792 | |
| 793 | static object * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 794 | builtin_hex(self, args) |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 795 | object *self; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 796 | object *args; |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 797 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 798 | object *v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 799 | number_methods *nb; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 800 | |
| 801 | if (!newgetargs(args, "O:hex", &v)) |
| 802 | return NULL; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 803 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 804 | if ((nb = v->ob_type->tp_as_number) == NULL || |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 805 | nb->nb_hex == NULL) { |
| 806 | err_setstr(TypeError, |
| 807 | "hex() argument can't be converted to hex"); |
| 808 | return NULL; |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 809 | } |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 810 | return (*nb->nb_hex)(v); |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 811 | } |
| 812 | |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 813 | static object *builtin_raw_input PROTO((object *, object *)); |
| 814 | |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 815 | static object * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 816 | builtin_input(self, args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 817 | object *self; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 818 | object *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 819 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 820 | object *line; |
| 821 | char *str; |
| 822 | object *res; |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 823 | object *globals, *locals; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 824 | |
| 825 | line = builtin_raw_input(self, args); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 826 | if (line == NULL) |
| 827 | return line; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 828 | if (!getargs(line, "s;embedded '\\0' in input line", &str)) |
| 829 | return NULL; |
| 830 | while (*str == ' ' || *str == '\t') |
| 831 | str++; |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 832 | globals = getglobals(); |
| 833 | locals = getlocals(); |
| 834 | if (dictlookup(globals, "__builtins__") == NULL) { |
| 835 | if (dictinsert(globals, "__builtins__", getbuiltins()) != 0) |
| 836 | return NULL; |
| 837 | } |
| 838 | res = run_string(str, eval_input, globals, locals); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 839 | DECREF(line); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 840 | return res; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Guido van Rossum | e8811f8 | 1997-02-14 15:48:05 +0000 | [diff] [blame] | 843 | static PyObject * |
| 844 | builtin_intern(self, args) |
| 845 | PyObject *self; |
| 846 | PyObject *args; |
| 847 | { |
| 848 | PyObject *s; |
| 849 | if (!PyArg_ParseTuple(args, "S", &s)) |
| 850 | return NULL; |
| 851 | Py_INCREF(s); |
| 852 | PyString_InternInPlace(&s); |
| 853 | return s; |
| 854 | } |
| 855 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 856 | static object * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 857 | builtin_int(self, args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 858 | object *self; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 859 | object *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 860 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 861 | object *v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 862 | number_methods *nb; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 863 | |
| 864 | if (!newgetargs(args, "O:int", &v)) |
| 865 | return NULL; |
| 866 | if ((nb = v->ob_type->tp_as_number) == NULL || |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 867 | nb->nb_int == NULL) { |
| 868 | err_setstr(TypeError, |
| 869 | "int() argument can't be converted to int"); |
| 870 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 871 | } |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 872 | return (*nb->nb_int)(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | static object * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 876 | builtin_len(self, args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 877 | object *self; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 878 | object *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 879 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 880 | object *v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 881 | long len; |
| 882 | typeobject *tp; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 883 | |
| 884 | if (!newgetargs(args, "O:len", &v)) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 885 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 886 | tp = v->ob_type; |
| 887 | if (tp->tp_as_sequence != NULL) { |
| 888 | len = (*tp->tp_as_sequence->sq_length)(v); |
| 889 | } |
| 890 | else if (tp->tp_as_mapping != NULL) { |
| 891 | len = (*tp->tp_as_mapping->mp_length)(v); |
| 892 | } |
| 893 | else { |
| 894 | err_setstr(TypeError, "len() of unsized object"); |
| 895 | return NULL; |
| 896 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 897 | if (len < 0) |
| 898 | return NULL; |
| 899 | else |
| 900 | return newintobject(len); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | static object * |
Guido van Rossum | d170577 | 1996-04-09 02:41:06 +0000 | [diff] [blame] | 904 | builtin_list(self, args) |
| 905 | object *self; |
| 906 | object *args; |
| 907 | { |
| 908 | object *v; |
| 909 | sequence_methods *sqf; |
| 910 | |
| 911 | if (!newgetargs(args, "O:list", &v)) |
| 912 | return NULL; |
| 913 | if ((sqf = v->ob_type->tp_as_sequence) != NULL) { |
| 914 | int n = (*sqf->sq_length)(v); |
| 915 | int i; |
| 916 | object *l; |
| 917 | if (n < 0) |
| 918 | return NULL; |
| 919 | l = newlistobject(n); |
| 920 | if (l == NULL) |
| 921 | return NULL; |
| 922 | for (i = 0; i < n; i++) { |
| 923 | object *item = (*sqf->sq_item)(v, i); |
| 924 | if (item == NULL) { |
| 925 | DECREF(l); |
| 926 | l = NULL; |
| 927 | break; |
| 928 | } |
| 929 | setlistitem(l, i, item); |
| 930 | } |
| 931 | /* XXX Should support indefinite-length sequences */ |
| 932 | return l; |
| 933 | } |
| 934 | err_setstr(TypeError, "list() argument must be a sequence"); |
| 935 | return NULL; |
| 936 | } |
| 937 | |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 938 | |
| 939 | static PyObject * |
| 940 | builtin_slice(self, args) |
| 941 | PyObject *self; |
| 942 | PyObject *args; |
| 943 | { |
| 944 | PyObject *start, *stop, *step; |
| 945 | |
| 946 | start = stop = step = NULL; |
| 947 | |
| 948 | if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step)) |
| 949 | return NULL; |
| 950 | |
| 951 | /*This swapping of stop and start is to maintain compatibility with |
| 952 | the range builtin.*/ |
| 953 | if (stop == NULL) { |
| 954 | stop = start; |
| 955 | start = NULL; |
| 956 | } |
| 957 | return PySlice_New(start, stop, step); |
| 958 | } |
| 959 | |
Guido van Rossum | d170577 | 1996-04-09 02:41:06 +0000 | [diff] [blame] | 960 | static object * |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 961 | builtin_locals(self, args) |
| 962 | object *self; |
| 963 | object *args; |
| 964 | { |
| 965 | object *d; |
| 966 | |
| 967 | if (!newgetargs(args, "")) |
| 968 | return NULL; |
| 969 | d = getlocals(); |
| 970 | INCREF(d); |
| 971 | return d; |
| 972 | } |
| 973 | |
| 974 | static object * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 975 | builtin_long(self, args) |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 976 | object *self; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 977 | object *args; |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 978 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 979 | object *v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 980 | number_methods *nb; |
| 981 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 982 | if (!newgetargs(args, "O:long", &v)) |
| 983 | return NULL; |
| 984 | if ((nb = v->ob_type->tp_as_number) == NULL || |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 985 | nb->nb_long == NULL) { |
| 986 | err_setstr(TypeError, |
| 987 | "long() argument can't be converted to long"); |
| 988 | return NULL; |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 989 | } |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 990 | return (*nb->nb_long)(v); |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | static object * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 994 | min_max(args, sign) |
| 995 | object *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 996 | int sign; |
| 997 | { |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 998 | int i; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 999 | object *v, *w, *x; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1000 | sequence_methods *sq; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1001 | |
| 1002 | if (gettuplesize(args) > 1) |
| 1003 | v = args; |
| 1004 | else if (!newgetargs(args, "O:min/max", &v)) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1005 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1006 | sq = v->ob_type->tp_as_sequence; |
| 1007 | if (sq == NULL) { |
| 1008 | err_setstr(TypeError, "min() or max() of non-sequence"); |
| 1009 | return NULL; |
| 1010 | } |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1011 | w = NULL; |
| 1012 | for (i = 0; ; i++) { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1013 | x = (*sq->sq_item)(v, i); /* Implies INCREF */ |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1014 | if (x == NULL) { |
| 1015 | if (err_occurred() == IndexError) { |
| 1016 | err_clear(); |
| 1017 | break; |
| 1018 | } |
| 1019 | XDECREF(w); |
| 1020 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1021 | } |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1022 | if (w == NULL) |
| 1023 | w = x; |
| 1024 | else { |
| 1025 | if (cmpobject(x, w) * sign > 0) { |
| 1026 | DECREF(w); |
| 1027 | w = x; |
| 1028 | } |
| 1029 | else |
| 1030 | DECREF(x); |
| 1031 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1032 | } |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1033 | if (w == NULL) |
| 1034 | err_setstr(ValueError, "min() or max() of empty sequence"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1035 | return w; |
| 1036 | } |
| 1037 | |
| 1038 | static object * |
| 1039 | builtin_min(self, v) |
| 1040 | object *self; |
| 1041 | object *v; |
| 1042 | { |
| 1043 | return min_max(v, -1); |
| 1044 | } |
| 1045 | |
| 1046 | static object * |
| 1047 | builtin_max(self, v) |
| 1048 | object *self; |
| 1049 | object *v; |
| 1050 | { |
| 1051 | return min_max(v, 1); |
| 1052 | } |
| 1053 | |
| 1054 | static object * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1055 | builtin_oct(self, args) |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 1056 | object *self; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1057 | object *args; |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 1058 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1059 | object *v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1060 | number_methods *nb; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1061 | |
| 1062 | if (!newgetargs(args, "O:oct", &v)) |
| 1063 | return NULL; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1064 | if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL || |
| 1065 | nb->nb_oct == NULL) { |
| 1066 | err_setstr(TypeError, |
| 1067 | "oct() argument can't be converted to oct"); |
| 1068 | return NULL; |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 1069 | } |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1070 | return (*nb->nb_oct)(v); |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | static object * |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 1074 | builtin_open(self, args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1075 | object *self; |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 1076 | object *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1077 | { |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1078 | char *name; |
| 1079 | char *mode = "r"; |
| 1080 | int bufsize = -1; |
| 1081 | object *f; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1082 | |
| 1083 | if (!newgetargs(args, "s|si:open", &name, &mode, &bufsize)) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1084 | return NULL; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1085 | f = newfileobject(name, mode); |
| 1086 | if (f != NULL) |
| 1087 | setfilebufsize(f, bufsize); |
| 1088 | return f; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | static object * |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 1092 | builtin_ord(self, args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1093 | object *self; |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 1094 | object *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1095 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1096 | char c; |
| 1097 | |
| 1098 | if (!newgetargs(args, "c:ord", &c)) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1099 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1100 | return newintobject((long)(c & 0xff)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | static object * |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1104 | do_pow(v, w) |
| 1105 | object *v, *w; |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 1106 | { |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1107 | object *res; |
| 1108 | if (is_instanceobject(v) || is_instanceobject(w)) |
| 1109 | return instancebinop(v, w, "__pow__", "__rpow__", do_pow); |
Guido van Rossum | c6bb8f7 | 1991-07-01 18:42:41 +0000 | [diff] [blame] | 1110 | if (v->ob_type->tp_as_number == NULL || |
Guido van Rossum | df05ac6 | 1994-08-29 12:52:37 +0000 | [diff] [blame] | 1111 | w->ob_type->tp_as_number == NULL) { |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 1112 | err_setstr(TypeError, "pow() requires numeric arguments"); |
| 1113 | return NULL; |
| 1114 | } |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 1115 | if ( |
| 1116 | #ifndef WITHOUT_COMPLEX |
| 1117 | !is_complexobject(v) && |
| 1118 | #endif |
| 1119 | is_floatobject(w) && getfloatvalue(v) < 0.0) { |
Guido van Rossum | 8a1e8eb | 1995-02-18 14:51:32 +0000 | [diff] [blame] | 1120 | if (!err_occurred()) |
| 1121 | err_setstr(ValueError, "negative number to float power"); |
Guido van Rossum | 2165158 | 1995-02-10 16:57:16 +0000 | [diff] [blame] | 1122 | return NULL; |
| 1123 | } |
Guido van Rossum | c6bb8f7 | 1991-07-01 18:42:41 +0000 | [diff] [blame] | 1124 | if (coerce(&v, &w) != 0) |
| 1125 | return NULL; |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1126 | res = (*v->ob_type->tp_as_number->nb_power)(v, w, None); |
Guido van Rossum | c6bb8f7 | 1991-07-01 18:42:41 +0000 | [diff] [blame] | 1127 | DECREF(v); |
| 1128 | DECREF(w); |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1129 | return res; |
| 1130 | } |
| 1131 | |
| 1132 | static object * |
| 1133 | builtin_pow(self, args) |
| 1134 | object *self; |
| 1135 | object *args; |
| 1136 | { |
| 1137 | object *v, *w, *z = None, *res; |
| 1138 | object *v1, *z1, *w2, *z2; |
| 1139 | |
| 1140 | if (!newgetargs(args, "OO|O:pow", &v, &w, &z)) |
| 1141 | return NULL; |
| 1142 | if (z == None) |
| 1143 | return do_pow(v, w); |
| 1144 | /* XXX The ternary version doesn't do class instance coercions */ |
| 1145 | if (is_instanceobject(v)) |
| 1146 | return v->ob_type->tp_as_number->nb_power(v, w, z); |
| 1147 | if (v->ob_type->tp_as_number == NULL || |
| 1148 | z->ob_type->tp_as_number == NULL || |
| 1149 | w->ob_type->tp_as_number == NULL) { |
| 1150 | err_setstr(TypeError, "pow() requires numeric arguments"); |
| 1151 | return NULL; |
| 1152 | } |
| 1153 | if (coerce(&v, &w) != 0) |
| 1154 | return NULL; |
| 1155 | res = NULL; |
| 1156 | v1 = v; |
| 1157 | z1 = z; |
| 1158 | if (coerce(&v1, &z1) != 0) |
| 1159 | goto error2; |
| 1160 | w2 = w; |
| 1161 | z2 = z1; |
| 1162 | if (coerce(&w2, &z2) != 0) |
| 1163 | goto error1; |
| 1164 | res = (*v1->ob_type->tp_as_number->nb_power)(v1, w2, z2); |
| 1165 | DECREF(w2); |
| 1166 | DECREF(z2); |
| 1167 | error1: |
| 1168 | DECREF(v1); |
| 1169 | DECREF(z1); |
| 1170 | error2: |
| 1171 | DECREF(v); |
| 1172 | DECREF(w); |
| 1173 | return res; |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | static object * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1177 | builtin_range(self, args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1178 | object *self; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1179 | object *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1180 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1181 | long ilow = 0, ihigh = 0, istep = 1; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1182 | int i, n; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1183 | object *v; |
| 1184 | |
| 1185 | if (gettuplesize(args) <= 1) { |
| 1186 | if (!newgetargs(args, |
Guido van Rossum | 0865dd9 | 1995-01-17 16:30:22 +0000 | [diff] [blame] | 1187 | "l;range() requires 1-3 int arguments", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1188 | &ihigh)) |
| 1189 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1190 | } |
| 1191 | else { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1192 | if (!newgetargs(args, |
Guido van Rossum | 0865dd9 | 1995-01-17 16:30:22 +0000 | [diff] [blame] | 1193 | "ll|l;range() requires 1-3 int arguments", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1194 | &ilow, &ihigh, &istep)) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1195 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1196 | } |
| 1197 | if (istep == 0) { |
Guido van Rossum | 50afb7a | 1991-12-10 13:52:31 +0000 | [diff] [blame] | 1198 | err_setstr(ValueError, "zero step for range()"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1199 | return NULL; |
| 1200 | } |
| 1201 | /* XXX ought to check overflow of subtraction */ |
| 1202 | if (istep > 0) |
| 1203 | n = (ihigh - ilow + istep - 1) / istep; |
| 1204 | else |
| 1205 | n = (ihigh - ilow + istep + 1) / istep; |
| 1206 | if (n < 0) |
| 1207 | n = 0; |
| 1208 | v = newlistobject(n); |
| 1209 | if (v == NULL) |
| 1210 | return NULL; |
| 1211 | for (i = 0; i < n; i++) { |
| 1212 | object *w = newintobject(ilow); |
| 1213 | if (w == NULL) { |
| 1214 | DECREF(v); |
| 1215 | return NULL; |
| 1216 | } |
| 1217 | setlistitem(v, i, w); |
| 1218 | ilow += istep; |
| 1219 | } |
| 1220 | return v; |
| 1221 | } |
| 1222 | |
| 1223 | static object * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1224 | builtin_xrange(self, args) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1225 | object *self; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1226 | object *args; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1227 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1228 | long ilow = 0, ihigh = 0, istep = 1; |
Guido van Rossum | 0865dd9 | 1995-01-17 16:30:22 +0000 | [diff] [blame] | 1229 | long n; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1230 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1231 | if (gettuplesize(args) <= 1) { |
| 1232 | if (!newgetargs(args, |
Guido van Rossum | 0865dd9 | 1995-01-17 16:30:22 +0000 | [diff] [blame] | 1233 | "l;xrange() requires 1-3 int arguments", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1234 | &ihigh)) |
| 1235 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1236 | } |
| 1237 | else { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1238 | if (!newgetargs(args, |
Guido van Rossum | 0865dd9 | 1995-01-17 16:30:22 +0000 | [diff] [blame] | 1239 | "ll|l;xrange() requires 1-3 int arguments", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1240 | &ilow, &ihigh, &istep)) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1241 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1242 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1243 | if (istep == 0) { |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1244 | err_setstr(ValueError, "zero step for xrange()"); |
| 1245 | return NULL; |
| 1246 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1247 | /* XXX ought to check overflow of subtraction */ |
| 1248 | if (istep > 0) |
| 1249 | n = (ihigh - ilow + istep - 1) / istep; |
| 1250 | else |
| 1251 | n = (ihigh - ilow + istep + 1) / istep; |
| 1252 | if (n < 0) |
| 1253 | n = 0; |
| 1254 | return newrangeobject(ilow, n, istep, 1); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1257 | extern char *my_readline PROTO((char *)); |
| 1258 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1259 | static object * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1260 | builtin_raw_input(self, args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1261 | object *self; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1262 | object *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1263 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1264 | object *v = NULL; |
| 1265 | object *f; |
| 1266 | |
| 1267 | if (!newgetargs(args, "|O:[raw_]input", &v)) |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1268 | return NULL; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1269 | if (getfilefile(sysget("stdin")) == stdin && |
Guido van Rossum | 53bb7ff | 1995-07-26 16:26:31 +0000 | [diff] [blame] | 1270 | getfilefile(sysget("stdout")) == stdout && |
| 1271 | isatty(fileno(stdin)) && isatty(fileno(stdout))) { |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1272 | object *po; |
| 1273 | char *prompt; |
| 1274 | char *s; |
| 1275 | object *result; |
| 1276 | if (v != NULL) { |
| 1277 | po = strobject(v); |
| 1278 | if (po == NULL) |
| 1279 | return NULL; |
| 1280 | prompt = getstringvalue(po); |
| 1281 | } |
| 1282 | else { |
| 1283 | po = NULL; |
| 1284 | prompt = ""; |
| 1285 | } |
| 1286 | s = my_readline(prompt); |
| 1287 | XDECREF(po); |
| 1288 | if (s == NULL) { |
| 1289 | err_set(KeyboardInterrupt); |
| 1290 | return NULL; |
| 1291 | } |
| 1292 | if (*s == '\0') { |
| 1293 | err_set(EOFError); |
| 1294 | result = NULL; |
| 1295 | } |
| 1296 | else { /* strip trailing '\n' */ |
| 1297 | result = newsizedstringobject(s, strlen(s)-1); |
| 1298 | } |
| 1299 | free(s); |
| 1300 | return result; |
| 1301 | } |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 1302 | if (v != NULL) { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1303 | f = sysget("stdout"); |
| 1304 | if (f == NULL) { |
| 1305 | err_setstr(RuntimeError, "lost sys.stdout"); |
| 1306 | return NULL; |
| 1307 | } |
| 1308 | flushline(); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1309 | if (writeobject(v, f, PRINT_RAW) != 0) |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 1310 | return NULL; |
| 1311 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1312 | f = sysget("stdin"); |
| 1313 | if (f == NULL) { |
| 1314 | err_setstr(RuntimeError, "lost sys.stdin"); |
| 1315 | return NULL; |
| 1316 | } |
| 1317 | return filegetline(f, -1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | static object * |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1321 | builtin_reduce(self, args) |
| 1322 | object *self; |
| 1323 | object *args; |
| 1324 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1325 | object *seq, *func, *result = NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1326 | sequence_methods *sqf; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1327 | register int i; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1328 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1329 | if (!newgetargs(args, "OO|O:reduce", &func, &seq, &result)) |
| 1330 | return NULL; |
| 1331 | if (result != NULL) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1332 | INCREF(result); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1333 | |
| 1334 | if ((sqf = seq->ob_type->tp_as_sequence) == NULL) { |
| 1335 | err_setstr(TypeError, |
| 1336 | "2nd argument to reduce() must be a sequence object"); |
| 1337 | return NULL; |
| 1338 | } |
| 1339 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1340 | if ((args = newtupleobject(2)) == NULL) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1341 | goto Fail; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1342 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1343 | for (i = 0; ; ++i) { |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1344 | object *op2; |
| 1345 | |
| 1346 | if (args->ob_refcnt > 1) { |
| 1347 | DECREF(args); |
| 1348 | if ((args = newtupleobject(2)) == NULL) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1349 | goto Fail; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1352 | if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) { |
| 1353 | if (err_occurred() == IndexError) { |
| 1354 | err_clear(); |
| 1355 | break; |
| 1356 | } |
| 1357 | goto Fail; |
| 1358 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1359 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1360 | if (result == NULL) |
| 1361 | result = op2; |
| 1362 | else { |
| 1363 | settupleitem(args, 0, result); |
| 1364 | settupleitem(args, 1, op2); |
| 1365 | if ((result = call_object(func, args)) == NULL) |
| 1366 | goto Fail; |
| 1367 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | DECREF(args); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1371 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1372 | if (result == NULL) |
| 1373 | err_setstr(TypeError, |
| 1374 | "reduce of empty sequence with no initial value"); |
| 1375 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1376 | return result; |
| 1377 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1378 | Fail: |
| 1379 | XDECREF(args); |
| 1380 | XDECREF(result); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1381 | return NULL; |
| 1382 | } |
| 1383 | |
| 1384 | static object * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1385 | builtin_reload(self, args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1386 | object *self; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1387 | object *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1388 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1389 | object *v; |
| 1390 | |
| 1391 | if (!newgetargs(args, "O:reload", &v)) |
| 1392 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1393 | return reload_module(v); |
| 1394 | } |
| 1395 | |
| 1396 | static object * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1397 | builtin_repr(self, args) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1398 | object *self; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1399 | object *args; |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1400 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1401 | object *v; |
| 1402 | |
| 1403 | if (!newgetargs(args, "O:repr", &v)) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1404 | return NULL; |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1405 | return reprobject(v); |
| 1406 | } |
| 1407 | |
| 1408 | static object * |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1409 | builtin_round(self, args) |
| 1410 | object *self; |
| 1411 | object *args; |
| 1412 | { |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1413 | double x; |
| 1414 | double f; |
| 1415 | int ndigits = 0; |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1416 | int i; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1417 | |
| 1418 | if (!newgetargs(args, "d|i:round", &x, &ndigits)) |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1419 | return NULL; |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1420 | f = 1.0; |
| 1421 | for (i = ndigits; --i >= 0; ) |
| 1422 | f = f*10.0; |
| 1423 | for (i = ndigits; ++i <= 0; ) |
| 1424 | f = f*0.1; |
| 1425 | if (x >= 0.0) |
| 1426 | return newfloatobject(floor(x*f + 0.5) / f); |
| 1427 | else |
| 1428 | return newfloatobject(ceil(x*f - 0.5) / f); |
| 1429 | } |
| 1430 | |
| 1431 | static object * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1432 | builtin_str(self, args) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1433 | object *self; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1434 | object *args; |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1435 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1436 | object *v; |
| 1437 | |
| 1438 | if (!newgetargs(args, "O:str", &v)) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1439 | return NULL; |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 1440 | return strobject(v); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | static object * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1444 | builtin_tuple(self, args) |
Guido van Rossum | cae027b | 1994-08-29 12:53:11 +0000 | [diff] [blame] | 1445 | object *self; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1446 | object *args; |
Guido van Rossum | cae027b | 1994-08-29 12:53:11 +0000 | [diff] [blame] | 1447 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1448 | object *v; |
Guido van Rossum | cae027b | 1994-08-29 12:53:11 +0000 | [diff] [blame] | 1449 | sequence_methods *sqf; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1450 | |
| 1451 | if (!newgetargs(args, "O:tuple", &v)) |
| 1452 | return NULL; |
Guido van Rossum | cae027b | 1994-08-29 12:53:11 +0000 | [diff] [blame] | 1453 | if (is_tupleobject(v)) { |
| 1454 | INCREF(v); |
| 1455 | return v; |
| 1456 | } |
Guido van Rossum | e4ab647 | 1994-08-30 12:38:05 +0000 | [diff] [blame] | 1457 | if (is_listobject(v)) |
| 1458 | return listtuple(v); |
Guido van Rossum | cae027b | 1994-08-29 12:53:11 +0000 | [diff] [blame] | 1459 | if (is_stringobject(v)) { |
| 1460 | int n = getstringsize(v); |
| 1461 | object *t = newtupleobject(n); |
| 1462 | if (t != NULL) { |
| 1463 | int i; |
| 1464 | char *p = getstringvalue(v); |
| 1465 | for (i = 0; i < n; i++) { |
| 1466 | object *item = newsizedstringobject(p+i, 1); |
| 1467 | if (item == NULL) { |
| 1468 | DECREF(t); |
| 1469 | t = NULL; |
| 1470 | break; |
| 1471 | } |
| 1472 | settupleitem(t, i, item); |
| 1473 | } |
| 1474 | } |
| 1475 | return t; |
| 1476 | } |
| 1477 | /* Generic sequence object */ |
| 1478 | if ((sqf = v->ob_type->tp_as_sequence) != NULL) { |
| 1479 | int n = (*sqf->sq_length)(v); |
| 1480 | int i; |
| 1481 | object *t; |
| 1482 | if (n < 0) |
| 1483 | return NULL; |
| 1484 | t = newtupleobject(n); |
| 1485 | if (t == NULL) |
| 1486 | return NULL; |
| 1487 | for (i = 0; i < n; i++) { |
| 1488 | object *item = (*sqf->sq_item)(v, i); |
| 1489 | if (item == NULL) { |
| 1490 | DECREF(t); |
| 1491 | t = NULL; |
| 1492 | break; |
| 1493 | } |
| 1494 | settupleitem(t, i, item); |
| 1495 | } |
| 1496 | /* XXX Should support indefinite-length sequences */ |
| 1497 | return t; |
| 1498 | } |
| 1499 | /* None of the above */ |
| 1500 | err_setstr(TypeError, "tuple() argument must be a sequence"); |
| 1501 | return NULL; |
| 1502 | } |
| 1503 | |
| 1504 | static object * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1505 | builtin_type(self, args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1506 | object *self; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1507 | object *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1508 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1509 | object *v; |
| 1510 | |
| 1511 | if (!newgetargs(args, "O:type", &v)) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1512 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1513 | v = (object *)v->ob_type; |
| 1514 | INCREF(v); |
| 1515 | return v; |
| 1516 | } |
| 1517 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1518 | static object * |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1519 | builtin_vars(self, args) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1520 | object *self; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1521 | object *args; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1522 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1523 | object *v = NULL; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1524 | object *d; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1525 | |
| 1526 | if (!newgetargs(args, "|O:vars", &v)) |
| 1527 | return NULL; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1528 | if (v == NULL) { |
| 1529 | d = getlocals(); |
Guido van Rossum | 53bb7ff | 1995-07-26 16:26:31 +0000 | [diff] [blame] | 1530 | if (d == NULL) { |
| 1531 | if (!err_occurred()) |
| 1532 | err_setstr(SystemError, "no locals!?"); |
| 1533 | } |
| 1534 | else |
| 1535 | INCREF(d); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1536 | } |
| 1537 | else { |
| 1538 | d = getattr(v, "__dict__"); |
| 1539 | if (d == NULL) { |
| 1540 | err_setstr(TypeError, |
| 1541 | "vars() argument must have __dict__ attribute"); |
| 1542 | return NULL; |
| 1543 | } |
| 1544 | } |
| 1545 | return d; |
| 1546 | } |
| 1547 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1548 | static struct methodlist builtin_methods[] = { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1549 | {"__import__", builtin___import__, 1}, |
| 1550 | {"abs", builtin_abs, 1}, |
| 1551 | {"apply", builtin_apply, 1}, |
| 1552 | {"callable", builtin_callable, 1}, |
| 1553 | {"chr", builtin_chr, 1}, |
| 1554 | {"cmp", builtin_cmp, 1}, |
| 1555 | {"coerce", builtin_coerce, 1}, |
| 1556 | {"compile", builtin_compile, 1}, |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 1557 | #ifndef WITHOUT_COMPLEX |
| 1558 | {"complex", builtin_complex, 1}, |
| 1559 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1560 | {"delattr", builtin_delattr, 1}, |
| 1561 | {"dir", builtin_dir, 1}, |
| 1562 | {"divmod", builtin_divmod, 1}, |
| 1563 | {"eval", builtin_eval, 1}, |
| 1564 | {"execfile", builtin_execfile, 1}, |
| 1565 | {"filter", builtin_filter, 1}, |
| 1566 | {"float", builtin_float, 1}, |
| 1567 | {"getattr", builtin_getattr, 1}, |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1568 | {"globals", builtin_globals, 1}, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1569 | {"hasattr", builtin_hasattr, 1}, |
| 1570 | {"hash", builtin_hash, 1}, |
| 1571 | {"hex", builtin_hex, 1}, |
| 1572 | {"id", builtin_id, 1}, |
| 1573 | {"input", builtin_input, 1}, |
Guido van Rossum | e8811f8 | 1997-02-14 15:48:05 +0000 | [diff] [blame] | 1574 | {"intern", builtin_intern, 1}, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1575 | {"int", builtin_int, 1}, |
| 1576 | {"len", builtin_len, 1}, |
Guido van Rossum | d170577 | 1996-04-09 02:41:06 +0000 | [diff] [blame] | 1577 | {"list", builtin_list, 1}, |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1578 | {"locals", builtin_locals, 1}, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1579 | {"long", builtin_long, 1}, |
| 1580 | {"map", builtin_map, 1}, |
| 1581 | {"max", builtin_max, 1}, |
| 1582 | {"min", builtin_min, 1}, |
| 1583 | {"oct", builtin_oct, 1}, |
| 1584 | {"open", builtin_open, 1}, |
| 1585 | {"ord", builtin_ord, 1}, |
Guido van Rossum | 030ae17 | 1994-11-10 22:33:19 +0000 | [diff] [blame] | 1586 | {"pow", builtin_pow, 1}, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1587 | {"range", builtin_range, 1}, |
| 1588 | {"raw_input", builtin_raw_input, 1}, |
| 1589 | {"reduce", builtin_reduce, 1}, |
| 1590 | {"reload", builtin_reload, 1}, |
| 1591 | {"repr", builtin_repr, 1}, |
| 1592 | {"round", builtin_round, 1}, |
| 1593 | {"setattr", builtin_setattr, 1}, |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1594 | {"slice", builtin_slice, 1}, |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1595 | {"str", builtin_str, 1}, |
| 1596 | {"tuple", builtin_tuple, 1}, |
| 1597 | {"type", builtin_type, 1}, |
| 1598 | {"vars", builtin_vars, 1}, |
| 1599 | {"xrange", builtin_xrange, 1}, |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 1600 | {NULL, NULL}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1601 | }; |
| 1602 | |
Guido van Rossum | 0865dd9 | 1995-01-17 16:30:22 +0000 | [diff] [blame] | 1603 | static object *builtin_mod; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1604 | static object *builtin_dict; |
| 1605 | |
| 1606 | object * |
Guido van Rossum | 0865dd9 | 1995-01-17 16:30:22 +0000 | [diff] [blame] | 1607 | getbuiltinmod() |
| 1608 | { |
| 1609 | return builtin_mod; |
| 1610 | } |
| 1611 | |
| 1612 | object * |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 1613 | getbuiltindict() |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1614 | { |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 1615 | return builtin_dict; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | /* Predefined exceptions */ |
| 1619 | |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1620 | object *AccessError; |
Guido van Rossum | fb905c3 | 1991-12-16 15:42:38 +0000 | [diff] [blame] | 1621 | object *AttributeError; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1622 | object *ConflictError; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1623 | object *EOFError; |
Guido van Rossum | fb905c3 | 1991-12-16 15:42:38 +0000 | [diff] [blame] | 1624 | object *IOError; |
Guido van Rossum | ed7711b | 1991-12-24 13:24:53 +0000 | [diff] [blame] | 1625 | object *ImportError; |
Guido van Rossum | fb905c3 | 1991-12-16 15:42:38 +0000 | [diff] [blame] | 1626 | object *IndexError; |
| 1627 | object *KeyError; |
| 1628 | object *KeyboardInterrupt; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1629 | object *MemoryError; |
| 1630 | object *NameError; |
Guido van Rossum | 50afb7a | 1991-12-10 13:52:31 +0000 | [diff] [blame] | 1631 | object *OverflowError; |
Guido van Rossum | fb905c3 | 1991-12-16 15:42:38 +0000 | [diff] [blame] | 1632 | object *RuntimeError; |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 1633 | object *SyntaxError; |
Guido van Rossum | fb905c3 | 1991-12-16 15:42:38 +0000 | [diff] [blame] | 1634 | object *SystemError; |
Guido van Rossum | 768a3f0 | 1991-12-31 13:13:47 +0000 | [diff] [blame] | 1635 | object *SystemExit; |
Guido van Rossum | fb905c3 | 1991-12-16 15:42:38 +0000 | [diff] [blame] | 1636 | object *TypeError; |
| 1637 | object *ValueError; |
| 1638 | object *ZeroDivisionError; |
Guido van Rossum | 50afb7a | 1991-12-10 13:52:31 +0000 | [diff] [blame] | 1639 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1640 | static object * |
Guido van Rossum | fb905c3 | 1991-12-16 15:42:38 +0000 | [diff] [blame] | 1641 | newstdexception(name) |
| 1642 | char *name; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1643 | { |
Guido van Rossum | fb905c3 | 1991-12-16 15:42:38 +0000 | [diff] [blame] | 1644 | object *v = newstringobject(name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1645 | if (v == NULL || dictinsert(builtin_dict, name, v) != 0) |
| 1646 | fatal("no mem for new standard exception"); |
| 1647 | return v; |
| 1648 | } |
| 1649 | |
| 1650 | static void |
| 1651 | initerrors() |
| 1652 | { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1653 | AccessError = newstdexception("AccessError"); |
Guido van Rossum | ed7711b | 1991-12-24 13:24:53 +0000 | [diff] [blame] | 1654 | AttributeError = newstdexception("AttributeError"); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1655 | ConflictError = newstdexception("ConflictError"); |
Guido van Rossum | fb905c3 | 1991-12-16 15:42:38 +0000 | [diff] [blame] | 1656 | EOFError = newstdexception("EOFError"); |
Guido van Rossum | ed7711b | 1991-12-24 13:24:53 +0000 | [diff] [blame] | 1657 | IOError = newstdexception("IOError"); |
| 1658 | ImportError = newstdexception("ImportError"); |
| 1659 | IndexError = newstdexception("IndexError"); |
| 1660 | KeyError = newstdexception("KeyError"); |
| 1661 | KeyboardInterrupt = newstdexception("KeyboardInterrupt"); |
Guido van Rossum | fb905c3 | 1991-12-16 15:42:38 +0000 | [diff] [blame] | 1662 | MemoryError = newstdexception("MemoryError"); |
| 1663 | NameError = newstdexception("NameError"); |
Guido van Rossum | fb905c3 | 1991-12-16 15:42:38 +0000 | [diff] [blame] | 1664 | OverflowError = newstdexception("OverflowError"); |
Guido van Rossum | ed7711b | 1991-12-24 13:24:53 +0000 | [diff] [blame] | 1665 | RuntimeError = newstdexception("RuntimeError"); |
Guido van Rossum | fb905c3 | 1991-12-16 15:42:38 +0000 | [diff] [blame] | 1666 | SyntaxError = newstdexception("SyntaxError"); |
Guido van Rossum | ed7711b | 1991-12-24 13:24:53 +0000 | [diff] [blame] | 1667 | SystemError = newstdexception("SystemError"); |
Guido van Rossum | 768a3f0 | 1991-12-31 13:13:47 +0000 | [diff] [blame] | 1668 | SystemExit = newstdexception("SystemExit"); |
Guido van Rossum | ed7711b | 1991-12-24 13:24:53 +0000 | [diff] [blame] | 1669 | TypeError = newstdexception("TypeError"); |
| 1670 | ValueError = newstdexception("ValueError"); |
| 1671 | ZeroDivisionError = newstdexception("ZeroDivisionError"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | void |
| 1675 | initbuiltin() |
| 1676 | { |
Guido van Rossum | 0865dd9 | 1995-01-17 16:30:22 +0000 | [diff] [blame] | 1677 | builtin_mod = initmodule("__builtin__", builtin_methods); |
| 1678 | builtin_dict = getmoduledict(builtin_mod); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1679 | INCREF(builtin_dict); |
| 1680 | initerrors(); |
| 1681 | (void) dictinsert(builtin_dict, "None", None); |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 1682 | (void) dictinsert(builtin_dict, "Ellipsis", Py_Ellipsis); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1683 | } |
Guido van Rossum | c6bb8f7 | 1991-07-01 18:42:41 +0000 | [diff] [blame] | 1684 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1685 | |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 1686 | /* Helper for filter(): filter a tuple through a function */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1687 | |
| 1688 | static object * |
| 1689 | filtertuple(func, tuple) |
| 1690 | object *func; |
| 1691 | object *tuple; |
| 1692 | { |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 1693 | object *result; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1694 | register int i, j; |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 1695 | int len = gettuplesize(tuple); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1696 | |
Guido van Rossum | b7b4562 | 1995-08-04 04:07:45 +0000 | [diff] [blame] | 1697 | if (len == 0) { |
| 1698 | INCREF(tuple); |
| 1699 | return tuple; |
| 1700 | } |
| 1701 | |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 1702 | if ((result = newtupleobject(len)) == NULL) |
| 1703 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1704 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1705 | for (i = j = 0; i < len; ++i) { |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 1706 | object *item, *good; |
| 1707 | int ok; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1708 | |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 1709 | if ((item = gettupleitem(tuple, i)) == NULL) |
| 1710 | goto Fail_1; |
| 1711 | if (func == None) { |
| 1712 | INCREF(item); |
| 1713 | good = item; |
| 1714 | } |
| 1715 | else { |
| 1716 | object *arg = mkvalue("(O)", item); |
| 1717 | if (arg == NULL) |
| 1718 | goto Fail_1; |
| 1719 | good = call_object(func, arg); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1720 | DECREF(arg); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 1721 | if (good == NULL) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1722 | goto Fail_1; |
| 1723 | } |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 1724 | ok = testbool(good); |
| 1725 | DECREF(good); |
| 1726 | if (ok) { |
| 1727 | INCREF(item); |
| 1728 | if (settupleitem(result, j++, item) < 0) |
| 1729 | goto Fail_1; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1730 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
Sjoerd Mullender | 615194a | 1993-11-01 13:46:50 +0000 | [diff] [blame] | 1733 | if (resizetuple(&result, j, 0) < 0) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1734 | return NULL; |
| 1735 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1736 | return result; |
| 1737 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1738 | Fail_1: |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 1739 | DECREF(result); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1740 | return NULL; |
| 1741 | } |
| 1742 | |
| 1743 | |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 1744 | /* Helper for filter(): filter a string through a function */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1745 | |
| 1746 | static object * |
| 1747 | filterstring(func, strobj) |
| 1748 | object *func; |
| 1749 | object *strobj; |
| 1750 | { |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 1751 | object *result; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1752 | register int i, j; |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 1753 | int len = getstringsize(strobj); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1754 | |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 1755 | if (func == None) { |
| 1756 | /* No character is ever false -- share input string */ |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1757 | INCREF(strobj); |
| 1758 | return strobj; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1759 | } |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 1760 | if ((result = newsizedstringobject(NULL, len)) == NULL) |
| 1761 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1762 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1763 | for (i = j = 0; i < len; ++i) { |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 1764 | object *item, *arg, *good; |
| 1765 | int ok; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1766 | |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 1767 | item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i); |
| 1768 | if (item == NULL) |
| 1769 | goto Fail_1; |
| 1770 | arg = mkvalue("(O)", item); |
| 1771 | DECREF(item); |
| 1772 | if (arg == NULL) |
| 1773 | goto Fail_1; |
| 1774 | good = call_object(func, arg); |
| 1775 | DECREF(arg); |
| 1776 | if (good == NULL) |
| 1777 | goto Fail_1; |
| 1778 | ok = testbool(good); |
| 1779 | DECREF(good); |
| 1780 | if (ok) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1781 | GETSTRINGVALUE((stringobject *)result)[j++] = |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 1782 | GETSTRINGVALUE((stringobject *)item)[0]; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
Guido van Rossum | 79d96d6 | 1996-08-16 20:44:34 +0000 | [diff] [blame] | 1785 | if (j < len && resizestring(&result, j) < 0) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1786 | return NULL; |
| 1787 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1788 | return result; |
| 1789 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1790 | Fail_1: |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 1791 | DECREF(result); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1792 | return NULL; |
| 1793 | } |