Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The |
| 3 | Netherlands. |
| 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" |
| 31 | #include "errcode.h" |
| 32 | #include "sysmodule.h" |
Guido van Rossum | 86cd6e6 | 1991-01-21 15:12:35 +0000 | [diff] [blame] | 33 | #include "bltinmodule.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 34 | #include "import.h" |
| 35 | #include "pythonrun.h" |
| 36 | #include "compile.h" /* For ceval.h */ |
| 37 | #include "ceval.h" |
| 38 | #include "modsupport.h" |
| 39 | |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 40 | /* Should be in longobject.h */ |
| 41 | extern stringobject *long_format PROTO((object *, int)); |
| 42 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 43 | static object * |
| 44 | builtin_abs(self, v) |
| 45 | object *self; |
| 46 | object *v; |
| 47 | { |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 48 | number_methods *nm; |
| 49 | if (v == NULL || (nm = v->ob_type->tp_as_number) == NULL) { |
| 50 | err_setstr(TypeError, "abs() requires numeric argument"); |
| 51 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 52 | } |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 53 | return (*nm->nb_absolute)(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static object * |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 57 | builtin_apply(self, v) |
| 58 | object *self; |
| 59 | object *v; |
| 60 | { |
| 61 | object *func, *args; |
| 62 | if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2) { |
| 63 | err_setstr(TypeError, "apply() requires (func,args)"); |
| 64 | return NULL; |
| 65 | } |
| 66 | func = gettupleitem(v, 0); |
| 67 | args = gettupleitem(v, 1); |
| 68 | return call_object(func, args); |
| 69 | } |
| 70 | |
| 71 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 72 | builtin_chr(self, v) |
| 73 | object *self; |
| 74 | object *v; |
| 75 | { |
| 76 | long x; |
| 77 | char s[1]; |
| 78 | if (v == NULL || !is_intobject(v)) { |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 79 | err_setstr(TypeError, "chr() requires int argument"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 80 | return NULL; |
| 81 | } |
| 82 | x = getintvalue(v); |
| 83 | if (x < 0 || x >= 256) { |
Guido van Rossum | 50afb7a | 1991-12-10 13:52:31 +0000 | [diff] [blame] | 84 | err_setstr(ValueError, "chr() arg not in range(256)"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 85 | return NULL; |
| 86 | } |
| 87 | s[0] = x; |
| 88 | return newsizedstringobject(s, 1); |
| 89 | } |
| 90 | |
| 91 | static object * |
| 92 | builtin_dir(self, v) |
| 93 | object *self; |
| 94 | object *v; |
| 95 | { |
| 96 | object *d; |
| 97 | if (v == NULL) { |
| 98 | d = getlocals(); |
Guido van Rossum | dc8a108 | 1991-10-20 20:11:03 +0000 | [diff] [blame] | 99 | INCREF(d); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 100 | } |
| 101 | else { |
Guido van Rossum | dc8a108 | 1991-10-20 20:11:03 +0000 | [diff] [blame] | 102 | d = getattr(v, "__dict__"); |
| 103 | if (d == NULL) { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 104 | err_setstr(TypeError, |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 105 | "dir() argument must have __dict__ attribute"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 106 | return NULL; |
| 107 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 108 | } |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 109 | if (is_dictobject(d)) { |
Guido van Rossum | dc8a108 | 1991-10-20 20:11:03 +0000 | [diff] [blame] | 110 | v = getdictkeys(d); |
| 111 | if (sortlist(v) != 0) { |
| 112 | DECREF(v); |
| 113 | v = NULL; |
| 114 | } |
| 115 | } |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 116 | else { |
| 117 | v = newlistobject(0); |
| 118 | } |
Guido van Rossum | dc8a108 | 1991-10-20 20:11:03 +0000 | [diff] [blame] | 119 | DECREF(d); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 120 | return v; |
| 121 | } |
| 122 | |
| 123 | static object * |
Guido van Rossum | c6bb8f7 | 1991-07-01 18:42:41 +0000 | [diff] [blame] | 124 | builtin_divmod(self, args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 125 | object *self; |
Guido van Rossum | c6bb8f7 | 1991-07-01 18:42:41 +0000 | [diff] [blame] | 126 | object *args; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 127 | { |
Guido van Rossum | c6bb8f7 | 1991-07-01 18:42:41 +0000 | [diff] [blame] | 128 | object *v, *w, *x; |
| 129 | if (args == NULL || !is_tupleobject(args) || gettuplesize(args) != 2) { |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 130 | err_setstr(TypeError, "divmod() requires 2 arguments"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 131 | return NULL; |
| 132 | } |
Guido van Rossum | c6bb8f7 | 1991-07-01 18:42:41 +0000 | [diff] [blame] | 133 | v = gettupleitem(args, 0); |
| 134 | w = gettupleitem(args, 1); |
| 135 | if (v->ob_type->tp_as_number == NULL || |
| 136 | w->ob_type->tp_as_number == NULL) { |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 137 | err_setstr(TypeError, "divmod() requires numeric arguments"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 138 | return NULL; |
| 139 | } |
Guido van Rossum | c6bb8f7 | 1991-07-01 18:42:41 +0000 | [diff] [blame] | 140 | if (coerce(&v, &w) != 0) |
| 141 | return NULL; |
| 142 | x = (*v->ob_type->tp_as_number->nb_divmod)(v, w); |
| 143 | DECREF(v); |
| 144 | DECREF(w); |
| 145 | return x; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static object * |
| 149 | exec_eval(v, start) |
| 150 | object *v; |
| 151 | int start; |
| 152 | { |
| 153 | object *str = NULL, *globals = NULL, *locals = NULL; |
| 154 | int n; |
| 155 | if (v != NULL) { |
| 156 | if (is_stringobject(v)) |
| 157 | str = v; |
| 158 | else if (is_tupleobject(v) && |
| 159 | ((n = gettuplesize(v)) == 2 || n == 3)) { |
| 160 | str = gettupleitem(v, 0); |
| 161 | globals = gettupleitem(v, 1); |
| 162 | if (n == 3) |
| 163 | locals = gettupleitem(v, 2); |
| 164 | } |
| 165 | } |
| 166 | if (str == NULL || !is_stringobject(str) || |
| 167 | globals != NULL && !is_dictobject(globals) || |
| 168 | locals != NULL && !is_dictobject(locals)) { |
| 169 | err_setstr(TypeError, |
| 170 | "exec/eval arguments must be string[,dict[,dict]]"); |
| 171 | return NULL; |
| 172 | } |
| 173 | return run_string(getstringvalue(str), start, globals, locals); |
| 174 | } |
| 175 | |
| 176 | static object * |
| 177 | builtin_eval(self, v) |
| 178 | object *self; |
| 179 | object *v; |
| 180 | { |
| 181 | return exec_eval(v, eval_input); |
| 182 | } |
| 183 | |
| 184 | static object * |
| 185 | builtin_exec(self, v) |
| 186 | object *self; |
| 187 | object *v; |
| 188 | { |
| 189 | return exec_eval(v, file_input); |
| 190 | } |
| 191 | |
| 192 | static object * |
| 193 | builtin_float(self, v) |
| 194 | object *self; |
| 195 | object *v; |
| 196 | { |
| 197 | if (v == NULL) { |
| 198 | /* */ |
| 199 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 200 | else if (is_intobject(v)) { |
| 201 | long x = getintvalue(v); |
| 202 | return newfloatobject((double)x); |
| 203 | } |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 204 | else if (is_longobject(v)) { |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 205 | return newfloatobject(dgetlongvalue(v)); |
| 206 | } |
| 207 | else if (is_floatobject(v)) { |
| 208 | INCREF(v); |
| 209 | return v; |
| 210 | } |
| 211 | err_setstr(TypeError, "float() argument must be int, long or float"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 212 | return NULL; |
| 213 | } |
| 214 | |
| 215 | static object * |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 216 | builtin_hex(self, v) |
| 217 | object *self; |
| 218 | object *v; |
| 219 | { |
| 220 | if (v != NULL) { |
| 221 | if (is_intobject(v)) { |
| 222 | char buf[20]; |
| 223 | long x = getintvalue(v); |
| 224 | if (x >= 0) |
| 225 | sprintf(buf, "0x%lx", x); |
| 226 | else |
| 227 | sprintf(buf, "-0x%lx", -x); |
| 228 | return newstringobject(buf); |
| 229 | } |
| 230 | if (is_longobject(v)) { |
| 231 | return (object *) long_format(v, 16); |
| 232 | } |
| 233 | } |
| 234 | err_setstr(TypeError, "hex() requires int/long argument"); |
| 235 | return NULL; |
| 236 | } |
| 237 | |
| 238 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 239 | builtin_input(self, v) |
| 240 | object *self; |
| 241 | object *v; |
| 242 | { |
| 243 | FILE *in = sysgetfile("stdin", stdin); |
| 244 | FILE *out = sysgetfile("stdout", stdout); |
| 245 | node *n; |
| 246 | int err; |
| 247 | object *m, *d; |
| 248 | flushline(); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 249 | if (v != NULL) { |
| 250 | if (printobject(v, out, PRINT_RAW) != 0) |
| 251 | return NULL; |
| 252 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 253 | m = add_module("__main__"); |
| 254 | d = getmoduledict(m); |
| 255 | return run_file(in, "<stdin>", expr_input, d, d); |
| 256 | } |
| 257 | |
| 258 | static object * |
| 259 | builtin_int(self, v) |
| 260 | object *self; |
| 261 | object *v; |
| 262 | { |
| 263 | if (v == NULL) { |
| 264 | /* */ |
| 265 | } |
| 266 | else if (is_intobject(v)) { |
| 267 | INCREF(v); |
| 268 | return v; |
| 269 | } |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 270 | else if (is_longobject(v)) { |
| 271 | long x; |
| 272 | x = getlongvalue(v); |
Guido van Rossum | ad40531 | 1991-06-03 10:58:01 +0000 | [diff] [blame] | 273 | if (err_occurred()) |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 274 | return NULL; |
| 275 | return newintobject(x); |
| 276 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 277 | else if (is_floatobject(v)) { |
| 278 | double x = getfloatvalue(v); |
Guido van Rossum | ad40531 | 1991-06-03 10:58:01 +0000 | [diff] [blame] | 279 | /* XXX should check for overflow */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 280 | return newintobject((long)x); |
| 281 | } |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 282 | err_setstr(TypeError, "int() argument must be int, long or float"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 283 | return NULL; |
| 284 | } |
| 285 | |
| 286 | static object * |
| 287 | builtin_len(self, v) |
| 288 | object *self; |
| 289 | object *v; |
| 290 | { |
| 291 | long len; |
| 292 | typeobject *tp; |
| 293 | if (v == NULL) { |
| 294 | err_setstr(TypeError, "len() without argument"); |
| 295 | return NULL; |
| 296 | } |
| 297 | tp = v->ob_type; |
| 298 | if (tp->tp_as_sequence != NULL) { |
| 299 | len = (*tp->tp_as_sequence->sq_length)(v); |
| 300 | } |
| 301 | else if (tp->tp_as_mapping != NULL) { |
| 302 | len = (*tp->tp_as_mapping->mp_length)(v); |
| 303 | } |
| 304 | else { |
| 305 | err_setstr(TypeError, "len() of unsized object"); |
| 306 | return NULL; |
| 307 | } |
| 308 | return newintobject(len); |
| 309 | } |
| 310 | |
| 311 | static object * |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 312 | builtin_long(self, v) |
| 313 | object *self; |
| 314 | object *v; |
| 315 | { |
| 316 | if (v == NULL) { |
| 317 | /* */ |
| 318 | } |
| 319 | else if (is_intobject(v)) { |
| 320 | return newlongobject(getintvalue(v)); |
| 321 | } |
| 322 | else if (is_longobject(v)) { |
| 323 | INCREF(v); |
| 324 | return v; |
| 325 | } |
| 326 | else if (is_floatobject(v)) { |
| 327 | double x = getfloatvalue(v); |
Guido van Rossum | ad40531 | 1991-06-03 10:58:01 +0000 | [diff] [blame] | 328 | return dnewlongobject(x); |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 329 | } |
| 330 | err_setstr(TypeError, "long() argument must be int, long or float"); |
| 331 | return NULL; |
| 332 | } |
| 333 | |
| 334 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 335 | min_max(v, sign) |
| 336 | object *v; |
| 337 | int sign; |
| 338 | { |
| 339 | int i, n, cmp; |
| 340 | object *w, *x; |
| 341 | sequence_methods *sq; |
| 342 | if (v == NULL) { |
| 343 | err_setstr(TypeError, "min() or max() without argument"); |
| 344 | return NULL; |
| 345 | } |
| 346 | sq = v->ob_type->tp_as_sequence; |
| 347 | if (sq == NULL) { |
| 348 | err_setstr(TypeError, "min() or max() of non-sequence"); |
| 349 | return NULL; |
| 350 | } |
| 351 | n = (*sq->sq_length)(v); |
| 352 | if (n == 0) { |
Guido van Rossum | 50afb7a | 1991-12-10 13:52:31 +0000 | [diff] [blame] | 353 | err_setstr(ValueError, "min() or max() of empty sequence"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 354 | return NULL; |
| 355 | } |
| 356 | w = (*sq->sq_item)(v, 0); /* Implies INCREF */ |
| 357 | for (i = 1; i < n; i++) { |
| 358 | x = (*sq->sq_item)(v, i); /* Implies INCREF */ |
| 359 | cmp = cmpobject(x, w); |
| 360 | if (cmp * sign > 0) { |
| 361 | DECREF(w); |
| 362 | w = x; |
| 363 | } |
| 364 | else |
| 365 | DECREF(x); |
| 366 | } |
| 367 | return w; |
| 368 | } |
| 369 | |
| 370 | static object * |
| 371 | builtin_min(self, v) |
| 372 | object *self; |
| 373 | object *v; |
| 374 | { |
| 375 | return min_max(v, -1); |
| 376 | } |
| 377 | |
| 378 | static object * |
| 379 | builtin_max(self, v) |
| 380 | object *self; |
| 381 | object *v; |
| 382 | { |
| 383 | return min_max(v, 1); |
| 384 | } |
| 385 | |
| 386 | static object * |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 387 | builtin_oct(self, v) |
| 388 | object *self; |
| 389 | object *v; |
| 390 | { |
| 391 | if (v != NULL) { |
| 392 | if (is_intobject(v)) { |
| 393 | char buf[20]; |
| 394 | long x = getintvalue(v); |
| 395 | if (x >= 0) |
| 396 | sprintf(buf, "0%lo", x); |
| 397 | else |
| 398 | sprintf(buf, "-0%lo", -x); |
| 399 | return newstringobject(buf); |
| 400 | } |
| 401 | if (is_longobject(v)) { |
| 402 | return (object *) long_format(v, 8); |
| 403 | } |
| 404 | } |
| 405 | err_setstr(TypeError, "oct() requires int/long argument"); |
| 406 | return NULL; |
| 407 | } |
| 408 | |
| 409 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 410 | builtin_open(self, v) |
| 411 | object *self; |
| 412 | object *v; |
| 413 | { |
| 414 | object *name, *mode; |
| 415 | if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2 || |
| 416 | !is_stringobject(name = gettupleitem(v, 0)) || |
| 417 | !is_stringobject(mode = gettupleitem(v, 1))) { |
| 418 | err_setstr(TypeError, "open() requires 2 string arguments"); |
| 419 | return NULL; |
| 420 | } |
| 421 | v = newfileobject(getstringvalue(name), getstringvalue(mode)); |
| 422 | return v; |
| 423 | } |
| 424 | |
| 425 | static object * |
| 426 | builtin_ord(self, v) |
| 427 | object *self; |
| 428 | object *v; |
| 429 | { |
| 430 | if (v == NULL || !is_stringobject(v)) { |
| 431 | err_setstr(TypeError, "ord() must have string argument"); |
| 432 | return NULL; |
| 433 | } |
| 434 | if (getstringsize(v) != 1) { |
Guido van Rossum | 50afb7a | 1991-12-10 13:52:31 +0000 | [diff] [blame] | 435 | err_setstr(ValueError, "ord() arg must have length 1"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 436 | return NULL; |
| 437 | } |
| 438 | return newintobject((long)(getstringvalue(v)[0] & 0xff)); |
| 439 | } |
| 440 | |
| 441 | static object * |
Guido van Rossum | c6bb8f7 | 1991-07-01 18:42:41 +0000 | [diff] [blame] | 442 | builtin_pow(self, args) |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 443 | object *self; |
Guido van Rossum | c6bb8f7 | 1991-07-01 18:42:41 +0000 | [diff] [blame] | 444 | object *args; |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 445 | { |
Guido van Rossum | c6bb8f7 | 1991-07-01 18:42:41 +0000 | [diff] [blame] | 446 | object *v, *w, *x; |
| 447 | if (args == NULL || !is_tupleobject(args) || gettuplesize(args) != 2) { |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 448 | err_setstr(TypeError, "pow() requires 2 arguments"); |
| 449 | return NULL; |
| 450 | } |
Guido van Rossum | c6bb8f7 | 1991-07-01 18:42:41 +0000 | [diff] [blame] | 451 | v = gettupleitem(args, 0); |
| 452 | w = gettupleitem(args, 1); |
| 453 | if (v->ob_type->tp_as_number == NULL || |
| 454 | w->ob_type->tp_as_number == NULL) { |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 455 | err_setstr(TypeError, "pow() requires numeric arguments"); |
| 456 | return NULL; |
| 457 | } |
Guido van Rossum | c6bb8f7 | 1991-07-01 18:42:41 +0000 | [diff] [blame] | 458 | if (coerce(&v, &w) != 0) |
| 459 | return NULL; |
| 460 | x = (*v->ob_type->tp_as_number->nb_power)(v, w); |
| 461 | DECREF(v); |
| 462 | DECREF(w); |
| 463 | return x; |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 467 | builtin_range(self, v) |
| 468 | object *self; |
| 469 | object *v; |
| 470 | { |
| 471 | static char *errmsg = "range() requires 1-3 int arguments"; |
| 472 | int i, n; |
| 473 | long ilow, ihigh, istep; |
| 474 | if (v != NULL && is_intobject(v)) { |
| 475 | ilow = 0; ihigh = getintvalue(v); istep = 1; |
| 476 | } |
| 477 | else if (v == NULL || !is_tupleobject(v)) { |
| 478 | err_setstr(TypeError, errmsg); |
| 479 | return NULL; |
| 480 | } |
| 481 | else { |
| 482 | n = gettuplesize(v); |
| 483 | if (n < 1 || n > 3) { |
| 484 | err_setstr(TypeError, errmsg); |
| 485 | return NULL; |
| 486 | } |
| 487 | for (i = 0; i < n; i++) { |
| 488 | if (!is_intobject(gettupleitem(v, i))) { |
| 489 | err_setstr(TypeError, errmsg); |
| 490 | return NULL; |
| 491 | } |
| 492 | } |
| 493 | if (n == 3) { |
| 494 | istep = getintvalue(gettupleitem(v, 2)); |
| 495 | --n; |
| 496 | } |
| 497 | else |
| 498 | istep = 1; |
| 499 | ihigh = getintvalue(gettupleitem(v, --n)); |
| 500 | if (n > 0) |
| 501 | ilow = getintvalue(gettupleitem(v, 0)); |
| 502 | else |
| 503 | ilow = 0; |
| 504 | } |
| 505 | if (istep == 0) { |
Guido van Rossum | 50afb7a | 1991-12-10 13:52:31 +0000 | [diff] [blame] | 506 | err_setstr(ValueError, "zero step for range()"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 507 | return NULL; |
| 508 | } |
| 509 | /* XXX ought to check overflow of subtraction */ |
| 510 | if (istep > 0) |
| 511 | n = (ihigh - ilow + istep - 1) / istep; |
| 512 | else |
| 513 | n = (ihigh - ilow + istep + 1) / istep; |
| 514 | if (n < 0) |
| 515 | n = 0; |
| 516 | v = newlistobject(n); |
| 517 | if (v == NULL) |
| 518 | return NULL; |
| 519 | for (i = 0; i < n; i++) { |
| 520 | object *w = newintobject(ilow); |
| 521 | if (w == NULL) { |
| 522 | DECREF(v); |
| 523 | return NULL; |
| 524 | } |
| 525 | setlistitem(v, i, w); |
| 526 | ilow += istep; |
| 527 | } |
| 528 | return v; |
| 529 | } |
| 530 | |
| 531 | static object * |
| 532 | builtin_raw_input(self, v) |
| 533 | object *self; |
| 534 | object *v; |
| 535 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 536 | FILE *out = sysgetfile("stdout", stdout); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 537 | flushline(); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 538 | if (v != NULL) { |
| 539 | if (printobject(v, out, PRINT_RAW) != 0) |
| 540 | return NULL; |
| 541 | } |
Guido van Rossum | 26203aa | 1991-04-04 15:20:41 +0000 | [diff] [blame] | 542 | return filegetline(sysget("stdin"), -1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | static object * |
| 546 | builtin_reload(self, v) |
| 547 | object *self; |
| 548 | object *v; |
| 549 | { |
| 550 | return reload_module(v); |
| 551 | } |
| 552 | |
| 553 | static object * |
| 554 | builtin_type(self, v) |
| 555 | object *self; |
| 556 | object *v; |
| 557 | { |
| 558 | if (v == NULL) { |
| 559 | err_setstr(TypeError, "type() requres an argument"); |
| 560 | return NULL; |
| 561 | } |
| 562 | v = (object *)v->ob_type; |
| 563 | INCREF(v); |
| 564 | return v; |
| 565 | } |
| 566 | |
| 567 | static struct methodlist builtin_methods[] = { |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 568 | {"abs", builtin_abs}, |
| 569 | {"apply", builtin_apply}, |
| 570 | {"chr", builtin_chr}, |
| 571 | {"dir", builtin_dir}, |
| 572 | {"divmod", builtin_divmod}, |
| 573 | {"eval", builtin_eval}, |
| 574 | {"exec", builtin_exec}, |
| 575 | {"float", builtin_float}, |
| 576 | {"hex", builtin_hex}, |
| 577 | {"input", builtin_input}, |
| 578 | {"int", builtin_int}, |
| 579 | {"len", builtin_len}, |
| 580 | {"long", builtin_long}, |
| 581 | {"max", builtin_max}, |
| 582 | {"min", builtin_min}, |
| 583 | {"oct", builtin_oct}, |
| 584 | {"open", builtin_open}, /* XXX move to OS module */ |
| 585 | {"ord", builtin_ord}, |
| 586 | {"pow", builtin_pow}, |
| 587 | {"range", builtin_range}, |
| 588 | {"raw_input", builtin_raw_input}, |
| 589 | {"reload", builtin_reload}, |
| 590 | {"type", builtin_type}, |
| 591 | {NULL, NULL}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 592 | }; |
| 593 | |
| 594 | static object *builtin_dict; |
| 595 | |
| 596 | object * |
| 597 | getbuiltin(name) |
Guido van Rossum | a57fb01 | 1991-08-16 08:54:58 +0000 | [diff] [blame] | 598 | object *name; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 599 | { |
Guido van Rossum | a57fb01 | 1991-08-16 08:54:58 +0000 | [diff] [blame] | 600 | return dict2lookup(builtin_dict, name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /* Predefined exceptions */ |
| 604 | |
Guido van Rossum | fb905c3 | 1991-12-16 15:42:38 +0000 | [diff] [blame] | 605 | object *AttributeError; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 606 | object *EOFError; |
Guido van Rossum | fb905c3 | 1991-12-16 15:42:38 +0000 | [diff] [blame] | 607 | object *IOError; |
Guido van Rossum | ed7711b | 1991-12-24 13:24:53 +0000 | [diff] [blame] | 608 | object *ImportError; |
Guido van Rossum | fb905c3 | 1991-12-16 15:42:38 +0000 | [diff] [blame] | 609 | object *IndexError; |
| 610 | object *KeyError; |
| 611 | object *KeyboardInterrupt; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 612 | object *MemoryError; |
| 613 | object *NameError; |
Guido van Rossum | 50afb7a | 1991-12-10 13:52:31 +0000 | [diff] [blame] | 614 | object *OverflowError; |
Guido van Rossum | fb905c3 | 1991-12-16 15:42:38 +0000 | [diff] [blame] | 615 | object *RuntimeError; |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 616 | object *SyntaxError; |
Guido van Rossum | fb905c3 | 1991-12-16 15:42:38 +0000 | [diff] [blame] | 617 | object *SystemError; |
| 618 | object *TypeError; |
| 619 | object *ValueError; |
| 620 | object *ZeroDivisionError; |
Guido van Rossum | 50afb7a | 1991-12-10 13:52:31 +0000 | [diff] [blame] | 621 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 622 | static object * |
Guido van Rossum | fb905c3 | 1991-12-16 15:42:38 +0000 | [diff] [blame] | 623 | newstdexception(name) |
| 624 | char *name; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 625 | { |
Guido van Rossum | fb905c3 | 1991-12-16 15:42:38 +0000 | [diff] [blame] | 626 | object *v = newstringobject(name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 627 | if (v == NULL || dictinsert(builtin_dict, name, v) != 0) |
| 628 | fatal("no mem for new standard exception"); |
| 629 | return v; |
| 630 | } |
| 631 | |
| 632 | static void |
| 633 | initerrors() |
| 634 | { |
Guido van Rossum | ed7711b | 1991-12-24 13:24:53 +0000 | [diff] [blame] | 635 | AttributeError = newstdexception("AttributeError"); |
Guido van Rossum | fb905c3 | 1991-12-16 15:42:38 +0000 | [diff] [blame] | 636 | EOFError = newstdexception("EOFError"); |
Guido van Rossum | ed7711b | 1991-12-24 13:24:53 +0000 | [diff] [blame] | 637 | IOError = newstdexception("IOError"); |
| 638 | ImportError = newstdexception("ImportError"); |
| 639 | IndexError = newstdexception("IndexError"); |
| 640 | KeyError = newstdexception("KeyError"); |
| 641 | KeyboardInterrupt = newstdexception("KeyboardInterrupt"); |
Guido van Rossum | fb905c3 | 1991-12-16 15:42:38 +0000 | [diff] [blame] | 642 | MemoryError = newstdexception("MemoryError"); |
| 643 | NameError = newstdexception("NameError"); |
Guido van Rossum | fb905c3 | 1991-12-16 15:42:38 +0000 | [diff] [blame] | 644 | OverflowError = newstdexception("OverflowError"); |
Guido van Rossum | ed7711b | 1991-12-24 13:24:53 +0000 | [diff] [blame] | 645 | RuntimeError = newstdexception("RuntimeError"); |
Guido van Rossum | fb905c3 | 1991-12-16 15:42:38 +0000 | [diff] [blame] | 646 | SyntaxError = newstdexception("SyntaxError"); |
Guido van Rossum | ed7711b | 1991-12-24 13:24:53 +0000 | [diff] [blame] | 647 | SystemError = newstdexception("SystemError"); |
| 648 | TypeError = newstdexception("TypeError"); |
| 649 | ValueError = newstdexception("ValueError"); |
| 650 | ZeroDivisionError = newstdexception("ZeroDivisionError"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | void |
| 654 | initbuiltin() |
| 655 | { |
| 656 | object *m; |
| 657 | m = initmodule("builtin", builtin_methods); |
| 658 | builtin_dict = getmoduledict(m); |
| 659 | INCREF(builtin_dict); |
| 660 | initerrors(); |
| 661 | (void) dictinsert(builtin_dict, "None", None); |
| 662 | } |
Guido van Rossum | c6bb8f7 | 1991-07-01 18:42:41 +0000 | [diff] [blame] | 663 | |
| 664 | /* Coerce two numeric types to the "larger" one. |
| 665 | Increment the reference count on each argument. |
| 666 | Return -1 and raise an exception if no coercion is possible |
| 667 | (and then no reference count is incremented). |
| 668 | XXX This should be distributed over the various numeric types, |
| 669 | XXX but for now I don't see how to implement that. |
| 670 | XXX So, for now, if you add a new numeric type, |
| 671 | XXX you must add to this function as well. */ |
| 672 | |
| 673 | int |
| 674 | coerce(pv, pw) |
| 675 | object **pv, **pw; |
| 676 | { |
| 677 | register object *v = *pv; |
| 678 | register object *w = *pw; |
| 679 | if (v->ob_type == w->ob_type) { |
| 680 | INCREF(v); |
| 681 | INCREF(w); |
| 682 | return 0; |
| 683 | } |
| 684 | if (v->ob_type->tp_as_number == NULL || |
| 685 | w->ob_type->tp_as_number == NULL) { |
| 686 | err_setstr(TypeError, "mixing number and non-number"); |
| 687 | return -1; |
| 688 | } |
| 689 | if (is_floatobject(v) || is_floatobject(w)) { |
| 690 | v = builtin_float((object *)0, v); |
| 691 | w = builtin_float((object *)0, w); |
| 692 | } |
| 693 | else if (is_longobject(v) || is_longobject(w)) { |
| 694 | v = builtin_long((object *)0, v); |
| 695 | w = builtin_long((object *)0, w); |
| 696 | } |
| 697 | else { |
| 698 | err_setstr(TypeError, "can't coerce numeric types?!?!?"); |
| 699 | return -1; |
| 700 | } |
| 701 | if (v == NULL || w == NULL) { |
| 702 | XDECREF(v); |
| 703 | XDECREF(w); |
| 704 | return -1; |
| 705 | } |
| 706 | *pv = v; |
| 707 | *pw = w; |
| 708 | return 0; |
| 709 | } |