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