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 | /* Execute compiled code */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 27 | #include "allobjects.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 29 | #include "import.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 30 | #include "sysmodule.h" |
| 31 | #include "compile.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 32 | #include "frameobject.h" |
| 33 | #include "ceval.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 34 | #include "opcode.h" |
Guido van Rossum | 86cd6e6 | 1991-01-21 15:12:35 +0000 | [diff] [blame] | 35 | #include "bltinmodule.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 36 | #include "traceback.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 38 | #ifndef NDEBUG |
| 39 | #define TRACE |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 40 | #endif |
| 41 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 42 | /* Forward declarations */ |
| 43 | /* XXX Need prototypes */ |
| 44 | |
| 45 | void flushline(); |
| 46 | static object *add(); |
| 47 | static object *sub(); |
| 48 | static object *mul(); |
| 49 | static object *divide(); |
| 50 | static object *rem(); |
| 51 | static object *neg(); |
| 52 | static object *pos(); |
| 53 | static object *not(); |
| 54 | static object *call_builtin(); |
| 55 | static object *call_function(); |
| 56 | static object *apply_subscript(); |
| 57 | static object *loop_subscript(); |
| 58 | static object *apply_slice(); |
| 59 | static object *cmp_outcome(); |
| 60 | static object *build_class(); |
| 61 | static int testbool(); |
| 62 | static int assign_subscript PROTO((object *, object *, object *)); |
| 63 | static int assign_slice PROTO((object *, object *, object *, object *)); |
| 64 | static int import_from PROTO((object *, object *, char *)); |
| 65 | |
| 66 | |
| 67 | static frameobject *current_frame; |
| 68 | |
| 69 | |
| 70 | /* Status code for main loop (reason for stack unwind) */ |
| 71 | |
| 72 | enum why_code { |
| 73 | WHY_NOT, /* No error */ |
| 74 | WHY_EXCEPTION, /* Exception occurred */ |
| 75 | WHY_RERAISE, /* Exception re-raised by 'finally' */ |
| 76 | WHY_RETURN, /* 'return' statement */ |
| 77 | WHY_BREAK /* 'break' statement */ |
| 78 | }; |
| 79 | |
| 80 | |
| 81 | /* Interpreter main loop */ |
| 82 | |
| 83 | object * |
| 84 | eval_code(co, globals, locals, arg) |
| 85 | codeobject *co; |
| 86 | object *globals; |
| 87 | object *locals; |
| 88 | object *arg; |
| 89 | { |
| 90 | register unsigned char *next_instr; |
| 91 | register int opcode; /* Current opcode */ |
| 92 | register int oparg; /* Current opcode argument, if any */ |
| 93 | register object **stack_pointer; |
| 94 | register enum why_code why; /* Reason for block stack unwind */ |
| 95 | register int err; /* Error status -- nonzero if error */ |
| 96 | register object *x; /* Result object -- NULL if error */ |
| 97 | register object *v; /* Temporary objects popped off stack */ |
| 98 | register object *w; |
| 99 | register object *u; |
| 100 | register object *t; |
| 101 | register frameobject *f; /* Current frame */ |
| 102 | int lineno; /* Current line number */ |
| 103 | object *retval; /* Return value iff why == WHY_RETURN */ |
| 104 | char *name; /* Name used by some instructions */ |
| 105 | FILE *fp; /* Used by print operations */ |
| 106 | #ifdef TRACE |
| 107 | int trace = dictlookup(globals, "__trace__") != NULL; |
| 108 | #endif |
| 109 | |
| 110 | /* Code access macros */ |
| 111 | |
| 112 | #define GETCONST(i) Getconst(f, i) |
| 113 | #define GETNAME(i) Getname(f, i) |
| 114 | #define GETNAMEV(i) Getnamev(f, i) |
| 115 | #define FIRST_INSTR() (GETUSTRINGVALUE(f->f_code->co_code)) |
| 116 | #define INSTR_OFFSET() (next_instr - FIRST_INSTR()) |
| 117 | #define NEXTOP() (*next_instr++) |
| 118 | #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2]) |
| 119 | #define JUMPTO(x) (next_instr = FIRST_INSTR() + (x)) |
| 120 | #define JUMPBY(x) (next_instr += (x)) |
| 121 | |
| 122 | /* Stack manipulation macros */ |
| 123 | |
| 124 | #define STACK_LEVEL() (stack_pointer - f->f_valuestack) |
| 125 | #define EMPTY() (STACK_LEVEL() == 0) |
| 126 | #define TOP() (stack_pointer[-1]) |
| 127 | #define BASIC_PUSH(v) (*stack_pointer++ = (v)) |
| 128 | #define BASIC_POP() (*--stack_pointer) |
| 129 | |
| 130 | #ifdef TRACE |
| 131 | #define PUSH(v) (BASIC_PUSH(v), trace && prtrace(TOP(), "push")) |
| 132 | #define POP() (trace && prtrace(TOP(), "pop"), BASIC_POP()) |
| 133 | #else |
| 134 | #define PUSH(v) BASIC_PUSH(v) |
| 135 | #define POP() BASIC_POP() |
| 136 | #endif |
| 137 | |
| 138 | f = newframeobject( |
| 139 | current_frame, /*back*/ |
| 140 | co, /*code*/ |
| 141 | globals, /*globals*/ |
| 142 | locals, /*locals*/ |
| 143 | 50, /*nvalues*/ |
| 144 | 20); /*nblocks*/ |
| 145 | if (f == NULL) |
| 146 | return NULL; |
| 147 | |
| 148 | current_frame = f; |
| 149 | |
| 150 | next_instr = GETUSTRINGVALUE(f->f_code->co_code); |
| 151 | |
| 152 | stack_pointer = f->f_valuestack; |
| 153 | |
| 154 | if (arg != NULL) { |
| 155 | INCREF(arg); |
| 156 | PUSH(arg); |
| 157 | } |
| 158 | |
| 159 | why = WHY_NOT; |
| 160 | err = 0; |
| 161 | x = None; /* Not a reference, just anything non-NULL */ |
| 162 | lineno = -1; |
| 163 | |
| 164 | for (;;) { |
Guido van Rossum | 4965bc8 | 1991-05-14 11:51:49 +0000 | [diff] [blame] | 165 | static int ticker; |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 166 | |
| 167 | /* Do periodic things */ |
| 168 | |
| 169 | if (--ticker < 0) { |
| 170 | ticker = 100; |
| 171 | if (intrcheck()) { |
| 172 | err_set(KeyboardInterrupt); |
| 173 | why = WHY_EXCEPTION; |
| 174 | tb_here(f, INSTR_OFFSET(), lineno); |
| 175 | goto on_error; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /* Extract opcode and argument */ |
| 180 | |
| 181 | opcode = NEXTOP(); |
| 182 | if (HAS_ARG(opcode)) |
| 183 | oparg = NEXTARG(); |
| 184 | |
| 185 | #ifdef TRACE |
| 186 | /* Instruction tracing */ |
| 187 | |
| 188 | if (trace) { |
| 189 | if (HAS_ARG(opcode)) { |
| 190 | printf("%d: %d, %d\n", |
| 191 | (int) (INSTR_OFFSET() - 3), |
| 192 | opcode, oparg); |
| 193 | } |
| 194 | else { |
| 195 | printf("%d: %d\n", |
| 196 | (int) (INSTR_OFFSET() - 1), opcode); |
| 197 | } |
| 198 | } |
| 199 | #endif |
| 200 | |
| 201 | /* Main switch on opcode */ |
| 202 | |
| 203 | switch (opcode) { |
| 204 | |
| 205 | /* BEWARE! |
| 206 | It is essential that any operation that fails sets either |
| 207 | x to NULL, err to nonzero, or why to anything but WHY_NOT, |
| 208 | and that no operation that succeeds does this! */ |
| 209 | |
| 210 | /* case STOP_CODE: this is an error! */ |
| 211 | |
| 212 | case POP_TOP: |
| 213 | v = POP(); |
| 214 | DECREF(v); |
| 215 | break; |
| 216 | |
| 217 | case ROT_TWO: |
| 218 | v = POP(); |
| 219 | w = POP(); |
| 220 | PUSH(v); |
| 221 | PUSH(w); |
| 222 | break; |
| 223 | |
| 224 | case ROT_THREE: |
| 225 | v = POP(); |
| 226 | w = POP(); |
| 227 | x = POP(); |
| 228 | PUSH(v); |
| 229 | PUSH(x); |
| 230 | PUSH(w); |
| 231 | break; |
| 232 | |
| 233 | case DUP_TOP: |
| 234 | v = TOP(); |
| 235 | INCREF(v); |
| 236 | PUSH(v); |
| 237 | break; |
| 238 | |
| 239 | case UNARY_POSITIVE: |
| 240 | v = POP(); |
| 241 | x = pos(v); |
| 242 | DECREF(v); |
| 243 | PUSH(x); |
| 244 | break; |
| 245 | |
| 246 | case UNARY_NEGATIVE: |
| 247 | v = POP(); |
| 248 | x = neg(v); |
| 249 | DECREF(v); |
| 250 | PUSH(x); |
| 251 | break; |
| 252 | |
| 253 | case UNARY_NOT: |
| 254 | v = POP(); |
| 255 | x = not(v); |
| 256 | DECREF(v); |
| 257 | PUSH(x); |
| 258 | break; |
| 259 | |
| 260 | case UNARY_CONVERT: |
| 261 | v = POP(); |
| 262 | x = reprobject(v); |
| 263 | DECREF(v); |
| 264 | PUSH(x); |
| 265 | break; |
| 266 | |
| 267 | case UNARY_CALL: |
| 268 | v = POP(); |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 269 | if (is_instancemethodobject(v) || is_funcobject(v)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 270 | x = call_function(v, (object *)NULL); |
| 271 | else |
| 272 | x = call_builtin(v, (object *)NULL); |
| 273 | DECREF(v); |
| 274 | PUSH(x); |
| 275 | break; |
| 276 | |
| 277 | case BINARY_MULTIPLY: |
| 278 | w = POP(); |
| 279 | v = POP(); |
| 280 | x = mul(v, w); |
| 281 | DECREF(v); |
| 282 | DECREF(w); |
| 283 | PUSH(x); |
| 284 | break; |
| 285 | |
| 286 | case BINARY_DIVIDE: |
| 287 | w = POP(); |
| 288 | v = POP(); |
| 289 | x = divide(v, w); |
| 290 | DECREF(v); |
| 291 | DECREF(w); |
| 292 | PUSH(x); |
| 293 | break; |
| 294 | |
| 295 | case BINARY_MODULO: |
| 296 | w = POP(); |
| 297 | v = POP(); |
| 298 | x = rem(v, w); |
| 299 | DECREF(v); |
| 300 | DECREF(w); |
| 301 | PUSH(x); |
| 302 | break; |
| 303 | |
| 304 | case BINARY_ADD: |
| 305 | w = POP(); |
| 306 | v = POP(); |
| 307 | x = add(v, w); |
| 308 | DECREF(v); |
| 309 | DECREF(w); |
| 310 | PUSH(x); |
| 311 | break; |
| 312 | |
| 313 | case BINARY_SUBTRACT: |
| 314 | w = POP(); |
| 315 | v = POP(); |
| 316 | x = sub(v, w); |
| 317 | DECREF(v); |
| 318 | DECREF(w); |
| 319 | PUSH(x); |
| 320 | break; |
| 321 | |
| 322 | case BINARY_SUBSCR: |
| 323 | w = POP(); |
| 324 | v = POP(); |
| 325 | x = apply_subscript(v, w); |
| 326 | DECREF(v); |
| 327 | DECREF(w); |
| 328 | PUSH(x); |
| 329 | break; |
| 330 | |
| 331 | case BINARY_CALL: |
| 332 | w = POP(); |
| 333 | v = POP(); |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 334 | if (is_instancemethodobject(v) || is_funcobject(v)) |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 335 | x = call_function(v, w); |
| 336 | else |
| 337 | x = call_builtin(v, w); |
| 338 | DECREF(v); |
| 339 | DECREF(w); |
| 340 | PUSH(x); |
| 341 | break; |
| 342 | |
| 343 | case SLICE+0: |
| 344 | case SLICE+1: |
| 345 | case SLICE+2: |
| 346 | case SLICE+3: |
| 347 | if ((opcode-SLICE) & 2) |
| 348 | w = POP(); |
| 349 | else |
| 350 | w = NULL; |
| 351 | if ((opcode-SLICE) & 1) |
| 352 | v = POP(); |
| 353 | else |
| 354 | v = NULL; |
| 355 | u = POP(); |
| 356 | x = apply_slice(u, v, w); |
| 357 | DECREF(u); |
| 358 | XDECREF(v); |
| 359 | XDECREF(w); |
| 360 | PUSH(x); |
| 361 | break; |
| 362 | |
| 363 | case STORE_SLICE+0: |
| 364 | case STORE_SLICE+1: |
| 365 | case STORE_SLICE+2: |
| 366 | case STORE_SLICE+3: |
| 367 | if ((opcode-STORE_SLICE) & 2) |
| 368 | w = POP(); |
| 369 | else |
| 370 | w = NULL; |
| 371 | if ((opcode-STORE_SLICE) & 1) |
| 372 | v = POP(); |
| 373 | else |
| 374 | v = NULL; |
| 375 | u = POP(); |
| 376 | t = POP(); |
| 377 | err = assign_slice(u, v, w, t); /* u[v:w] = t */ |
| 378 | DECREF(t); |
| 379 | DECREF(u); |
| 380 | XDECREF(v); |
| 381 | XDECREF(w); |
| 382 | break; |
| 383 | |
| 384 | case DELETE_SLICE+0: |
| 385 | case DELETE_SLICE+1: |
| 386 | case DELETE_SLICE+2: |
| 387 | case DELETE_SLICE+3: |
| 388 | if ((opcode-DELETE_SLICE) & 2) |
| 389 | w = POP(); |
| 390 | else |
| 391 | w = NULL; |
| 392 | if ((opcode-DELETE_SLICE) & 1) |
| 393 | v = POP(); |
| 394 | else |
| 395 | v = NULL; |
| 396 | u = POP(); |
| 397 | err = assign_slice(u, v, w, (object *)NULL); |
| 398 | /* del u[v:w] */ |
| 399 | DECREF(u); |
| 400 | XDECREF(v); |
| 401 | XDECREF(w); |
| 402 | break; |
| 403 | |
| 404 | case STORE_SUBSCR: |
| 405 | w = POP(); |
| 406 | v = POP(); |
| 407 | u = POP(); |
| 408 | /* v[w] = u */ |
| 409 | err = assign_subscript(v, w, u); |
| 410 | DECREF(u); |
| 411 | DECREF(v); |
| 412 | DECREF(w); |
| 413 | break; |
| 414 | |
| 415 | case DELETE_SUBSCR: |
| 416 | w = POP(); |
| 417 | v = POP(); |
| 418 | /* del v[w] */ |
| 419 | err = assign_subscript(v, w, (object *)NULL); |
| 420 | DECREF(v); |
| 421 | DECREF(w); |
| 422 | break; |
| 423 | |
| 424 | case PRINT_EXPR: |
| 425 | v = POP(); |
| 426 | fp = sysgetfile("stdout", stdout); |
| 427 | /* Print value except if procedure result */ |
| 428 | if (v != None) { |
| 429 | flushline(); |
| 430 | softspace(sysget("stdout"), 1); |
| 431 | printobject(v, fp, 0); |
| 432 | flushline(); |
| 433 | } |
| 434 | DECREF(v); |
| 435 | break; |
| 436 | |
| 437 | case PRINT_ITEM: |
| 438 | v = POP(); |
| 439 | fp = sysgetfile("stdout", stdout); |
| 440 | if (softspace(sysget("stdout"), 1)) |
| 441 | fprintf(fp, " "); |
| 442 | if (is_stringobject(v)) { |
| 443 | char *s = getstringvalue(v); |
| 444 | int len = getstringsize(v); |
| 445 | fwrite(s, 1, len, fp); |
| 446 | if (len > 0 && s[len-1] == '\n') |
| 447 | softspace(sysget("stdout"), 0); |
| 448 | } |
| 449 | else { |
| 450 | printobject(v, fp, 0); |
| 451 | } |
| 452 | DECREF(v); |
| 453 | break; |
| 454 | |
| 455 | case PRINT_NEWLINE: |
| 456 | fp = sysgetfile("stdout", stdout); |
| 457 | fprintf(fp, "\n"); |
| 458 | softspace(sysget("stdout"), 0); |
| 459 | break; |
| 460 | |
| 461 | case BREAK_LOOP: |
| 462 | why = WHY_BREAK; |
| 463 | break; |
| 464 | |
| 465 | case RAISE_EXCEPTION: |
| 466 | v = POP(); |
| 467 | w = POP(); |
| 468 | if (!is_stringobject(w)) |
| 469 | err_setstr(TypeError, |
| 470 | "exceptions must be strings"); |
| 471 | else |
| 472 | err_setval(w, v); |
| 473 | DECREF(v); |
| 474 | DECREF(w); |
| 475 | why = WHY_EXCEPTION; |
| 476 | break; |
| 477 | |
| 478 | case LOAD_LOCALS: |
| 479 | v = f->f_locals; |
| 480 | INCREF(v); |
| 481 | PUSH(v); |
| 482 | break; |
| 483 | |
| 484 | case RETURN_VALUE: |
| 485 | retval = POP(); |
| 486 | why = WHY_RETURN; |
| 487 | break; |
| 488 | |
| 489 | case REQUIRE_ARGS: |
| 490 | if (EMPTY()) { |
| 491 | err_setstr(TypeError, |
| 492 | "function expects argument(s)"); |
| 493 | why = WHY_EXCEPTION; |
| 494 | } |
| 495 | break; |
| 496 | |
| 497 | case REFUSE_ARGS: |
| 498 | if (!EMPTY()) { |
| 499 | err_setstr(TypeError, |
| 500 | "function expects no argument(s)"); |
| 501 | why = WHY_EXCEPTION; |
| 502 | } |
| 503 | break; |
| 504 | |
| 505 | case BUILD_FUNCTION: |
| 506 | v = POP(); |
| 507 | x = newfuncobject(v, f->f_globals); |
| 508 | DECREF(v); |
| 509 | PUSH(x); |
| 510 | break; |
| 511 | |
| 512 | case POP_BLOCK: |
| 513 | { |
| 514 | block *b = pop_block(f); |
| 515 | while (STACK_LEVEL() > b->b_level) { |
| 516 | v = POP(); |
| 517 | DECREF(v); |
| 518 | } |
| 519 | } |
| 520 | break; |
| 521 | |
| 522 | case END_FINALLY: |
| 523 | v = POP(); |
| 524 | if (is_intobject(v)) { |
| 525 | why = (enum why_code) getintvalue(v); |
| 526 | if (why == WHY_RETURN) |
| 527 | retval = POP(); |
| 528 | } |
| 529 | else if (is_stringobject(v)) { |
| 530 | w = POP(); |
| 531 | err_setval(v, w); |
| 532 | DECREF(w); |
| 533 | w = POP(); |
| 534 | tb_store(w); |
| 535 | DECREF(w); |
| 536 | why = WHY_RERAISE; |
| 537 | } |
| 538 | else if (v != None) { |
| 539 | err_setstr(SystemError, |
| 540 | "'finally' pops bad exception"); |
| 541 | why = WHY_EXCEPTION; |
| 542 | } |
| 543 | DECREF(v); |
| 544 | break; |
| 545 | |
| 546 | case BUILD_CLASS: |
| 547 | w = POP(); |
| 548 | v = POP(); |
| 549 | x = build_class(v, w); |
| 550 | PUSH(x); |
| 551 | DECREF(v); |
| 552 | DECREF(w); |
| 553 | break; |
| 554 | |
| 555 | case STORE_NAME: |
| 556 | w = GETNAMEV(oparg); |
| 557 | v = POP(); |
| 558 | err = dict2insert(f->f_locals, w, v); |
| 559 | DECREF(v); |
| 560 | break; |
| 561 | |
| 562 | case DELETE_NAME: |
| 563 | name = GETNAME(oparg); |
| 564 | if ((err = dictremove(f->f_locals, name)) != 0) |
| 565 | err_setstr(NameError, name); |
| 566 | break; |
| 567 | |
| 568 | case UNPACK_TUPLE: |
| 569 | v = POP(); |
| 570 | if (!is_tupleobject(v)) { |
| 571 | err_setstr(TypeError, "unpack non-tuple"); |
| 572 | why = WHY_EXCEPTION; |
| 573 | } |
| 574 | else if (gettuplesize(v) != oparg) { |
| 575 | err_setstr(RuntimeError, |
| 576 | "unpack tuple of wrong size"); |
| 577 | why = WHY_EXCEPTION; |
| 578 | } |
| 579 | else { |
| 580 | for (; --oparg >= 0; ) { |
| 581 | w = gettupleitem(v, oparg); |
| 582 | INCREF(w); |
| 583 | PUSH(w); |
| 584 | } |
| 585 | } |
| 586 | DECREF(v); |
| 587 | break; |
| 588 | |
| 589 | case UNPACK_LIST: |
| 590 | v = POP(); |
| 591 | if (!is_listobject(v)) { |
| 592 | err_setstr(TypeError, "unpack non-list"); |
| 593 | why = WHY_EXCEPTION; |
| 594 | } |
| 595 | else if (getlistsize(v) != oparg) { |
| 596 | err_setstr(RuntimeError, |
| 597 | "unpack list of wrong size"); |
| 598 | why = WHY_EXCEPTION; |
| 599 | } |
| 600 | else { |
| 601 | for (; --oparg >= 0; ) { |
| 602 | w = getlistitem(v, oparg); |
| 603 | INCREF(w); |
| 604 | PUSH(w); |
| 605 | } |
| 606 | } |
| 607 | DECREF(v); |
| 608 | break; |
| 609 | |
| 610 | case STORE_ATTR: |
| 611 | name = GETNAME(oparg); |
| 612 | v = POP(); |
| 613 | u = POP(); |
| 614 | err = setattr(v, name, u); /* v.name = u */ |
| 615 | DECREF(v); |
| 616 | DECREF(u); |
| 617 | break; |
| 618 | |
| 619 | case DELETE_ATTR: |
| 620 | name = GETNAME(oparg); |
| 621 | v = POP(); |
| 622 | err = setattr(v, name, (object *)NULL); |
| 623 | /* del v.name */ |
| 624 | DECREF(v); |
| 625 | break; |
| 626 | |
| 627 | case LOAD_CONST: |
| 628 | x = GETCONST(oparg); |
| 629 | INCREF(x); |
| 630 | PUSH(x); |
| 631 | break; |
| 632 | |
| 633 | case LOAD_NAME: |
| 634 | name = GETNAME(oparg); |
| 635 | x = dictlookup(f->f_locals, name); |
| 636 | if (x == NULL) { |
| 637 | x = dictlookup(f->f_globals, name); |
| 638 | if (x == NULL) { |
| 639 | x = getbuiltin(name); |
| 640 | if (x == NULL) { |
| 641 | err_setstr(NameError, name); |
| 642 | break; |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | INCREF(x); |
| 647 | PUSH(x); |
| 648 | break; |
| 649 | |
| 650 | case LOAD_GLOBAL: |
| 651 | name = GETNAME(oparg); |
| 652 | x = dictlookup(f->f_globals, name); |
| 653 | if (x == NULL) { |
| 654 | x = getbuiltin(name); |
| 655 | if (x == NULL) { |
| 656 | err_setstr(NameError, name); |
| 657 | break; |
| 658 | } |
| 659 | } |
| 660 | INCREF(x); |
| 661 | PUSH(x); |
| 662 | break; |
| 663 | |
| 664 | case LOAD_LOCAL: |
| 665 | name = GETNAME(oparg); |
| 666 | x = dictlookup(f->f_locals, name); |
| 667 | if (x == NULL) { |
| 668 | err_setstr(NameError, name); |
| 669 | break; |
| 670 | } |
| 671 | INCREF(x); |
| 672 | PUSH(x); |
| 673 | break; |
| 674 | |
| 675 | case BUILD_TUPLE: |
| 676 | x = newtupleobject(oparg); |
| 677 | if (x != NULL) { |
| 678 | for (; --oparg >= 0;) { |
| 679 | w = POP(); |
| 680 | err = settupleitem(x, oparg, w); |
| 681 | if (err != 0) |
| 682 | break; |
| 683 | } |
| 684 | PUSH(x); |
| 685 | } |
| 686 | break; |
| 687 | |
| 688 | case BUILD_LIST: |
| 689 | x = newlistobject(oparg); |
| 690 | if (x != NULL) { |
| 691 | for (; --oparg >= 0;) { |
| 692 | w = POP(); |
| 693 | err = setlistitem(x, oparg, w); |
| 694 | if (err != 0) |
| 695 | break; |
| 696 | } |
| 697 | PUSH(x); |
| 698 | } |
| 699 | break; |
| 700 | |
| 701 | case BUILD_MAP: |
| 702 | x = newdictobject(); |
| 703 | PUSH(x); |
| 704 | break; |
| 705 | |
| 706 | case LOAD_ATTR: |
| 707 | name = GETNAME(oparg); |
| 708 | v = POP(); |
| 709 | x = getattr(v, name); |
| 710 | DECREF(v); |
| 711 | PUSH(x); |
| 712 | break; |
| 713 | |
| 714 | case COMPARE_OP: |
| 715 | w = POP(); |
| 716 | v = POP(); |
| 717 | x = cmp_outcome((enum cmp_op)oparg, v, w); |
| 718 | DECREF(v); |
| 719 | DECREF(w); |
| 720 | PUSH(x); |
| 721 | break; |
| 722 | |
| 723 | case IMPORT_NAME: |
| 724 | name = GETNAME(oparg); |
| 725 | x = import_module(name); |
| 726 | XINCREF(x); |
| 727 | PUSH(x); |
| 728 | break; |
| 729 | |
| 730 | case IMPORT_FROM: |
| 731 | name = GETNAME(oparg); |
| 732 | v = TOP(); |
| 733 | err = import_from(f->f_locals, v, name); |
| 734 | break; |
| 735 | |
| 736 | case JUMP_FORWARD: |
| 737 | JUMPBY(oparg); |
| 738 | break; |
| 739 | |
| 740 | case JUMP_IF_FALSE: |
| 741 | if (!testbool(TOP())) |
| 742 | JUMPBY(oparg); |
| 743 | break; |
| 744 | |
| 745 | case JUMP_IF_TRUE: |
| 746 | if (testbool(TOP())) |
| 747 | JUMPBY(oparg); |
| 748 | break; |
| 749 | |
| 750 | case JUMP_ABSOLUTE: |
| 751 | JUMPTO(oparg); |
| 752 | break; |
| 753 | |
| 754 | case FOR_LOOP: |
| 755 | /* for v in s: ... |
| 756 | On entry: stack contains s, i. |
| 757 | On exit: stack contains s, i+1, s[i]; |
| 758 | but if loop exhausted: |
| 759 | s, i are popped, and we jump */ |
| 760 | w = POP(); /* Loop index */ |
| 761 | v = POP(); /* Sequence object */ |
| 762 | u = loop_subscript(v, w); |
| 763 | if (u != NULL) { |
| 764 | PUSH(v); |
| 765 | x = newintobject(getintvalue(w)+1); |
| 766 | PUSH(x); |
| 767 | DECREF(w); |
| 768 | PUSH(u); |
| 769 | } |
| 770 | else { |
| 771 | DECREF(v); |
| 772 | DECREF(w); |
| 773 | /* A NULL can mean "s exhausted" |
| 774 | but also an error: */ |
| 775 | if (err_occurred()) |
| 776 | why = WHY_EXCEPTION; |
| 777 | else |
| 778 | JUMPBY(oparg); |
| 779 | } |
| 780 | break; |
| 781 | |
| 782 | case SETUP_LOOP: |
| 783 | case SETUP_EXCEPT: |
| 784 | case SETUP_FINALLY: |
| 785 | setup_block(f, opcode, INSTR_OFFSET() + oparg, |
| 786 | STACK_LEVEL()); |
| 787 | break; |
| 788 | |
| 789 | case SET_LINENO: |
| 790 | #ifdef TRACE |
| 791 | if (trace) |
| 792 | printf("--- Line %d ---\n", oparg); |
| 793 | #endif |
| 794 | lineno = oparg; |
| 795 | break; |
| 796 | |
| 797 | default: |
| 798 | fprintf(stderr, |
| 799 | "XXX lineno: %d, opcode: %d\n", |
| 800 | lineno, opcode); |
| 801 | err_setstr(SystemError, "eval_code: unknown opcode"); |
| 802 | why = WHY_EXCEPTION; |
| 803 | break; |
| 804 | |
| 805 | } /* switch */ |
| 806 | |
| 807 | on_error: |
| 808 | |
| 809 | /* Quickly continue if no error occurred */ |
| 810 | |
| 811 | if (why == WHY_NOT) { |
| 812 | if (err == 0 && x != NULL) |
| 813 | continue; /* Normal, fast path */ |
| 814 | why = WHY_EXCEPTION; |
| 815 | x = None; |
| 816 | err = 0; |
| 817 | } |
| 818 | |
| 819 | #ifndef NDEBUG |
| 820 | /* Double-check exception status */ |
| 821 | |
| 822 | if (why == WHY_EXCEPTION || why == WHY_RERAISE) { |
| 823 | if (!err_occurred()) { |
| 824 | fprintf(stderr, "XXX ghost error\n"); |
| 825 | err_setstr(SystemError, "ghost error"); |
| 826 | why = WHY_EXCEPTION; |
| 827 | } |
| 828 | } |
| 829 | else { |
| 830 | if (err_occurred()) { |
| 831 | fprintf(stderr, "XXX undetected error\n"); |
| 832 | why = WHY_EXCEPTION; |
| 833 | } |
| 834 | } |
| 835 | #endif |
| 836 | |
| 837 | /* Log traceback info if this is a real exception */ |
| 838 | |
| 839 | if (why == WHY_EXCEPTION) { |
| 840 | int lasti = INSTR_OFFSET() - 1; |
| 841 | if (HAS_ARG(opcode)) |
| 842 | lasti -= 2; |
| 843 | tb_here(f, lasti, lineno); |
| 844 | } |
| 845 | |
| 846 | /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */ |
| 847 | |
| 848 | if (why == WHY_RERAISE) |
| 849 | why = WHY_EXCEPTION; |
| 850 | |
| 851 | /* Unwind stacks if a (pseudo) exception occurred */ |
| 852 | |
| 853 | while (why != WHY_NOT && f->f_iblock > 0) { |
| 854 | block *b = pop_block(f); |
| 855 | while (STACK_LEVEL() > b->b_level) { |
| 856 | v = POP(); |
| 857 | XDECREF(v); |
| 858 | } |
| 859 | if (b->b_type == SETUP_LOOP && why == WHY_BREAK) { |
| 860 | why = WHY_NOT; |
| 861 | JUMPTO(b->b_handler); |
| 862 | break; |
| 863 | } |
| 864 | if (b->b_type == SETUP_FINALLY || |
| 865 | b->b_type == SETUP_EXCEPT && |
| 866 | why == WHY_EXCEPTION) { |
| 867 | if (why == WHY_EXCEPTION) { |
| 868 | object *exc, *val; |
| 869 | err_get(&exc, &val); |
| 870 | if (val == NULL) { |
| 871 | val = None; |
| 872 | INCREF(val); |
| 873 | } |
| 874 | v = tb_fetch(); |
| 875 | /* Make the raw exception data |
| 876 | available to the handler, |
| 877 | so a program can emulate the |
| 878 | Python main loop. Don't do |
| 879 | this for 'finally'. */ |
| 880 | if (b->b_type == SETUP_EXCEPT) { |
| 881 | #if 0 /* Oops, this breaks too many things */ |
| 882 | sysset("exc_traceback", v); |
| 883 | #endif |
| 884 | sysset("exc_value", val); |
| 885 | sysset("exc_type", exc); |
| 886 | err_clear(); |
| 887 | } |
| 888 | PUSH(v); |
| 889 | PUSH(val); |
| 890 | PUSH(exc); |
| 891 | } |
| 892 | else { |
| 893 | if (why == WHY_RETURN) |
| 894 | PUSH(retval); |
| 895 | v = newintobject((long)why); |
| 896 | PUSH(v); |
| 897 | } |
| 898 | why = WHY_NOT; |
| 899 | JUMPTO(b->b_handler); |
| 900 | break; |
| 901 | } |
| 902 | } /* unwind stack */ |
| 903 | |
| 904 | /* End the loop if we still have an error (or return) */ |
| 905 | |
| 906 | if (why != WHY_NOT) |
| 907 | break; |
| 908 | |
| 909 | } /* main loop */ |
| 910 | |
| 911 | /* Pop remaining stack entries */ |
| 912 | |
| 913 | while (!EMPTY()) { |
| 914 | v = POP(); |
| 915 | XDECREF(v); |
| 916 | } |
| 917 | |
| 918 | /* Restore previous frame and release the current one */ |
| 919 | |
| 920 | current_frame = f->f_back; |
| 921 | DECREF(f); |
| 922 | |
| 923 | if (why == WHY_RETURN) |
| 924 | return retval; |
| 925 | else |
| 926 | return NULL; |
| 927 | } |
| 928 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 929 | #ifdef TRACE |
| 930 | static int |
| 931 | prtrace(v, str) |
| 932 | object *v; |
| 933 | char *str; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 934 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 935 | printf("%s ", str); |
| 936 | printobject(v, stdout, 0); |
| 937 | printf("\n"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 938 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 939 | #endif |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 940 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 941 | object * |
| 942 | getlocals() |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 943 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 944 | if (current_frame == NULL) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 945 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 946 | else |
| 947 | return current_frame->f_locals; |
| 948 | } |
| 949 | |
| 950 | object * |
| 951 | getglobals() |
| 952 | { |
| 953 | if (current_frame == NULL) |
| 954 | return NULL; |
| 955 | else |
| 956 | return current_frame->f_globals; |
| 957 | } |
| 958 | |
| 959 | void |
| 960 | printtraceback(fp) |
| 961 | FILE *fp; |
| 962 | { |
| 963 | object *v = tb_fetch(); |
| 964 | if (v != NULL) { |
| 965 | fprintf(fp, "Stack backtrace (innermost last):\n"); |
| 966 | tb_print(v, fp); |
| 967 | DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 968 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 972 | void |
| 973 | flushline() |
| 974 | { |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 975 | if (softspace(sysget("stdout"), 0)) |
| 976 | fprintf(sysgetfile("stdout", stdout), "\n"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 979 | |
| 980 | /* Test a value used as condition, e.g., in a for or if statement */ |
| 981 | |
| 982 | static int |
| 983 | testbool(v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 984 | object *v; |
| 985 | { |
Guido van Rossum | 4965bc8 | 1991-05-14 11:51:49 +0000 | [diff] [blame] | 986 | if (v == None) |
| 987 | return 0; |
| 988 | if (v->ob_type->tp_as_number != NULL) |
| 989 | return (*v->ob_type->tp_as_number->nb_nonzero)(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 990 | if (v->ob_type->tp_as_sequence != NULL) |
| 991 | return (*v->ob_type->tp_as_sequence->sq_length)(v) != 0; |
| 992 | if (v->ob_type->tp_as_mapping != NULL) |
| 993 | return (*v->ob_type->tp_as_mapping->mp_length)(v) != 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 994 | /* All other objects are 'true' */ |
| 995 | return 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 999 | add(v, w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1000 | object *v, *w; |
| 1001 | { |
| 1002 | if (v->ob_type->tp_as_number != NULL) |
| 1003 | v = (*v->ob_type->tp_as_number->nb_add)(v, w); |
| 1004 | else if (v->ob_type->tp_as_sequence != NULL) |
| 1005 | v = (*v->ob_type->tp_as_sequence->sq_concat)(v, w); |
| 1006 | else { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1007 | err_setstr(TypeError, "+ not supported by operands"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1008 | return NULL; |
| 1009 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1010 | return v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1014 | sub(v, w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1015 | object *v, *w; |
| 1016 | { |
| 1017 | if (v->ob_type->tp_as_number != NULL) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1018 | return (*v->ob_type->tp_as_number->nb_subtract)(v, w); |
| 1019 | err_setstr(TypeError, "bad operand type(s) for -"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1020 | return NULL; |
| 1021 | } |
| 1022 | |
| 1023 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1024 | mul(v, w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1025 | object *v, *w; |
| 1026 | { |
| 1027 | typeobject *tp; |
| 1028 | if (is_intobject(v) && w->ob_type->tp_as_sequence != NULL) { |
| 1029 | /* int*sequence -- swap v and w */ |
| 1030 | object *tmp = v; |
| 1031 | v = w; |
| 1032 | w = tmp; |
| 1033 | } |
| 1034 | tp = v->ob_type; |
| 1035 | if (tp->tp_as_number != NULL) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1036 | return (*tp->tp_as_number->nb_multiply)(v, w); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1037 | if (tp->tp_as_sequence != NULL) { |
| 1038 | if (!is_intobject(w)) { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1039 | err_setstr(TypeError, |
| 1040 | "can't multiply sequence with non-int"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1041 | return NULL; |
| 1042 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1043 | return (*tp->tp_as_sequence->sq_repeat) |
| 1044 | (v, (int)getintvalue(w)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1045 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1046 | err_setstr(TypeError, "bad operand type(s) for *"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1047 | return NULL; |
| 1048 | } |
| 1049 | |
| 1050 | static object * |
Guido van Rossum | 40d0b7e | 1990-12-20 23:03:11 +0000 | [diff] [blame] | 1051 | divide(v, w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1052 | object *v, *w; |
| 1053 | { |
| 1054 | if (v->ob_type->tp_as_number != NULL) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1055 | return (*v->ob_type->tp_as_number->nb_divide)(v, w); |
| 1056 | err_setstr(TypeError, "bad operand type(s) for /"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1057 | return NULL; |
| 1058 | } |
| 1059 | |
| 1060 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1061 | rem(v, w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1062 | object *v, *w; |
| 1063 | { |
| 1064 | if (v->ob_type->tp_as_number != NULL) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1065 | return (*v->ob_type->tp_as_number->nb_remainder)(v, w); |
| 1066 | err_setstr(TypeError, "bad operand type(s) for %"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1067 | return NULL; |
| 1068 | } |
| 1069 | |
| 1070 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1071 | neg(v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1072 | object *v; |
| 1073 | { |
| 1074 | if (v->ob_type->tp_as_number != NULL) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1075 | return (*v->ob_type->tp_as_number->nb_negative)(v); |
| 1076 | err_setstr(TypeError, "bad operand type(s) for unary -"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1077 | return NULL; |
| 1078 | } |
| 1079 | |
| 1080 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1081 | pos(v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1082 | object *v; |
| 1083 | { |
| 1084 | if (v->ob_type->tp_as_number != NULL) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1085 | return (*v->ob_type->tp_as_number->nb_positive)(v); |
| 1086 | err_setstr(TypeError, "bad operand type(s) for unary +"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1087 | return NULL; |
| 1088 | } |
| 1089 | |
| 1090 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1091 | not(v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1092 | object *v; |
| 1093 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1094 | int outcome = testbool(v); |
| 1095 | object *w = outcome == 0 ? True : False; |
| 1096 | INCREF(w); |
| 1097 | return w; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1101 | call_builtin(func, arg) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1102 | object *func; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1103 | object *arg; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1104 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1105 | if (is_methodobject(func)) { |
| 1106 | method meth = getmethod(func); |
| 1107 | object *self = getself(func); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1108 | return (*meth)(self, arg); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1109 | } |
| 1110 | if (is_classobject(func)) { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1111 | if (arg != NULL) { |
| 1112 | err_setstr(TypeError, |
| 1113 | "classobject() allows no arguments"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1114 | return NULL; |
| 1115 | } |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1116 | return newinstanceobject(func); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1117 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1118 | err_setstr(TypeError, "call of non-function"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1119 | return NULL; |
| 1120 | } |
| 1121 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1122 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1123 | call_function(func, arg) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1124 | object *func; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1125 | object *arg; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1126 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1127 | object *newarg = NULL; |
| 1128 | object *newlocals, *newglobals; |
| 1129 | object *co, *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1130 | |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 1131 | if (is_instancemethodobject(func)) { |
| 1132 | object *self = instancemethodgetself(func); |
| 1133 | func = instancemethodgetfunc(func); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1134 | if (arg == NULL) { |
| 1135 | arg = self; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1136 | } |
| 1137 | else { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1138 | newarg = newtupleobject(2); |
| 1139 | if (newarg == NULL) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1140 | return NULL; |
| 1141 | INCREF(self); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1142 | INCREF(arg); |
| 1143 | settupleitem(newarg, 0, self); |
| 1144 | settupleitem(newarg, 1, arg); |
| 1145 | arg = newarg; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1146 | } |
| 1147 | } |
| 1148 | else { |
| 1149 | if (!is_funcobject(func)) { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1150 | err_setstr(TypeError, "call of non-function"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1151 | return NULL; |
| 1152 | } |
| 1153 | } |
| 1154 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1155 | co = getfunccode(func); |
| 1156 | if (co == NULL) { |
| 1157 | XDECREF(newarg); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1158 | return NULL; |
| 1159 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1160 | if (!is_codeobject(co)) { |
| 1161 | fprintf(stderr, "XXX Bad code\n"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1162 | abort(); |
| 1163 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1164 | newlocals = newdictobject(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1165 | if (newlocals == NULL) { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1166 | XDECREF(newarg); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1167 | return NULL; |
| 1168 | } |
| 1169 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1170 | newglobals = getfuncglobals(func); |
| 1171 | INCREF(newglobals); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1172 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1173 | v = eval_code((codeobject *)co, newglobals, newlocals, arg); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1174 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1175 | DECREF(newlocals); |
| 1176 | DECREF(newglobals); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1177 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1178 | XDECREF(newarg); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1179 | |
| 1180 | return v; |
| 1181 | } |
| 1182 | |
| 1183 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1184 | apply_subscript(v, w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1185 | object *v, *w; |
| 1186 | { |
| 1187 | typeobject *tp = v->ob_type; |
| 1188 | if (tp->tp_as_sequence == NULL && tp->tp_as_mapping == NULL) { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1189 | err_setstr(TypeError, "unsubscriptable object"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1190 | return NULL; |
| 1191 | } |
| 1192 | if (tp->tp_as_sequence != NULL) { |
| 1193 | int i; |
| 1194 | if (!is_intobject(w)) { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1195 | err_setstr(TypeError, "sequence subscript not int"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1196 | return NULL; |
| 1197 | } |
| 1198 | i = getintvalue(w); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1199 | return (*tp->tp_as_sequence->sq_item)(v, i); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1200 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1201 | return (*tp->tp_as_mapping->mp_subscript)(v, w); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1205 | loop_subscript(v, w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1206 | object *v, *w; |
| 1207 | { |
| 1208 | sequence_methods *sq = v->ob_type->tp_as_sequence; |
| 1209 | int i, n; |
| 1210 | if (sq == NULL) { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1211 | err_setstr(TypeError, "loop over non-sequence"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1212 | return NULL; |
| 1213 | } |
| 1214 | i = getintvalue(w); |
| 1215 | n = (*sq->sq_length)(v); |
| 1216 | if (i >= n) |
| 1217 | return NULL; /* End of loop */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1218 | return (*sq->sq_item)(v, i); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | static int |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1222 | slice_index(v, isize, pi) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1223 | object *v; |
| 1224 | int isize; |
| 1225 | int *pi; |
| 1226 | { |
| 1227 | if (v != NULL) { |
| 1228 | if (!is_intobject(v)) { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1229 | err_setstr(TypeError, "slice index must be int"); |
| 1230 | return -1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1231 | } |
| 1232 | *pi = getintvalue(v); |
| 1233 | if (*pi < 0) |
| 1234 | *pi += isize; |
| 1235 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1236 | return 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1240 | apply_slice(u, v, w) /* return u[v:w] */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1241 | object *u, *v, *w; |
| 1242 | { |
| 1243 | typeobject *tp = u->ob_type; |
| 1244 | int ilow, ihigh, isize; |
| 1245 | if (tp->tp_as_sequence == NULL) { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1246 | err_setstr(TypeError, "only sequences can be sliced"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1247 | return NULL; |
| 1248 | } |
| 1249 | ilow = 0; |
| 1250 | isize = ihigh = (*tp->tp_as_sequence->sq_length)(u); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1251 | if (slice_index(v, isize, &ilow) != 0) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1252 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1253 | if (slice_index(w, isize, &ihigh) != 0) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1254 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1255 | return (*tp->tp_as_sequence->sq_slice)(u, ilow, ihigh); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1256 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1257 | |
| 1258 | static int |
| 1259 | assign_subscript(w, key, v) /* w[key] = v */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1260 | object *w; |
| 1261 | object *key; |
| 1262 | object *v; |
| 1263 | { |
| 1264 | typeobject *tp = w->ob_type; |
| 1265 | sequence_methods *sq; |
| 1266 | mapping_methods *mp; |
| 1267 | int (*func)(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1268 | if ((sq = tp->tp_as_sequence) != NULL && |
| 1269 | (func = sq->sq_ass_item) != NULL) { |
| 1270 | if (!is_intobject(key)) { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1271 | err_setstr(TypeError, |
| 1272 | "sequence subscript must be integer"); |
| 1273 | return -1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1274 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1275 | else |
| 1276 | return (*func)(w, (int)getintvalue(key), v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1277 | } |
| 1278 | else if ((mp = tp->tp_as_mapping) != NULL && |
| 1279 | (func = mp->mp_ass_subscript) != NULL) { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1280 | return (*func)(w, key, v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1281 | } |
| 1282 | else { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1283 | err_setstr(TypeError, |
| 1284 | "can't assign to this subscripted object"); |
| 1285 | return -1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1286 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1289 | static int |
| 1290 | assign_slice(u, v, w, x) /* u[v:w] = x */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1291 | object *u, *v, *w, *x; |
| 1292 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1293 | sequence_methods *sq = u->ob_type->tp_as_sequence; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1294 | int ilow, ihigh, isize; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1295 | if (sq == NULL) { |
| 1296 | err_setstr(TypeError, "assign to slice of non-sequence"); |
| 1297 | return -1; |
| 1298 | } |
| 1299 | if (sq == NULL || sq->sq_ass_slice == NULL) { |
| 1300 | err_setstr(TypeError, "unassignable slice"); |
| 1301 | return -1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1302 | } |
| 1303 | ilow = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1304 | isize = ihigh = (*sq->sq_length)(u); |
| 1305 | if (slice_index(v, isize, &ilow) != 0) |
| 1306 | return -1; |
| 1307 | if (slice_index(w, isize, &ihigh) != 0) |
| 1308 | return -1; |
| 1309 | return (*sq->sq_ass_slice)(u, ilow, ihigh, x); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | static int |
| 1313 | cmp_exception(err, v) |
| 1314 | object *err, *v; |
| 1315 | { |
| 1316 | if (is_tupleobject(v)) { |
| 1317 | int i, n; |
| 1318 | n = gettuplesize(v); |
| 1319 | for (i = 0; i < n; i++) { |
| 1320 | if (err == gettupleitem(v, i)) |
| 1321 | return 1; |
| 1322 | } |
| 1323 | return 0; |
| 1324 | } |
| 1325 | return err == v; |
| 1326 | } |
| 1327 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1328 | static int |
| 1329 | cmp_member(v, w) |
| 1330 | object *v, *w; |
| 1331 | { |
| 1332 | int i, n, cmp; |
| 1333 | object *x; |
| 1334 | sequence_methods *sq; |
| 1335 | /* Special case for char in string */ |
| 1336 | if (is_stringobject(w)) { |
| 1337 | register char *s, *end; |
| 1338 | register char c; |
| 1339 | if (!is_stringobject(v) || getstringsize(v) != 1) { |
| 1340 | err_setstr(TypeError, |
| 1341 | "string member test needs char left operand"); |
| 1342 | return -1; |
| 1343 | } |
| 1344 | c = getstringvalue(v)[0]; |
| 1345 | s = getstringvalue(w); |
| 1346 | end = s + getstringsize(w); |
| 1347 | while (s < end) { |
| 1348 | if (c == *s++) |
| 1349 | return 1; |
| 1350 | } |
| 1351 | return 0; |
| 1352 | } |
| 1353 | sq = w->ob_type->tp_as_sequence; |
| 1354 | if (sq == NULL) { |
| 1355 | err_setstr(TypeError, |
| 1356 | "'in' or 'not in' needs sequence right argument"); |
| 1357 | return -1; |
| 1358 | } |
| 1359 | n = (*sq->sq_length)(w); |
| 1360 | for (i = 0; i < n; i++) { |
| 1361 | x = (*sq->sq_item)(w, i); |
| 1362 | cmp = cmpobject(v, x); |
| 1363 | XDECREF(x); |
| 1364 | if (cmp == 0) |
| 1365 | return 1; |
| 1366 | } |
| 1367 | return 0; |
| 1368 | } |
| 1369 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1370 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1371 | cmp_outcome(op, v, w) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1372 | enum cmp_op op; |
| 1373 | register object *v; |
| 1374 | register object *w; |
| 1375 | { |
| 1376 | register int cmp; |
| 1377 | register int res = 0; |
| 1378 | switch (op) { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1379 | case IS: |
| 1380 | case IS_NOT: |
| 1381 | res = (v == w); |
| 1382 | if (op == IS_NOT) |
| 1383 | res = !res; |
| 1384 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1385 | case IN: |
| 1386 | case NOT_IN: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1387 | res = cmp_member(v, w); |
| 1388 | if (res < 0) |
| 1389 | return NULL; |
| 1390 | if (op == NOT_IN) |
| 1391 | res = !res; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1392 | break; |
| 1393 | case EXC_MATCH: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1394 | res = cmp_exception(v, w); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1395 | break; |
| 1396 | default: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1397 | cmp = cmpobject(v, w); |
| 1398 | switch (op) { |
| 1399 | case LT: res = cmp < 0; break; |
| 1400 | case LE: res = cmp <= 0; break; |
| 1401 | case EQ: res = cmp == 0; break; |
| 1402 | case NE: res = cmp != 0; break; |
| 1403 | case GT: res = cmp > 0; break; |
| 1404 | case GE: res = cmp >= 0; break; |
| 1405 | /* XXX no default? (res is initialized to 0 though) */ |
| 1406 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1407 | } |
| 1408 | v = res ? True : False; |
| 1409 | INCREF(v); |
| 1410 | return v; |
| 1411 | } |
| 1412 | |
Guido van Rossum | 374a922 | 1991-04-04 10:40:29 +0000 | [diff] [blame] | 1413 | /* XXX This function should use dict2 variants (change interface!) */ |
| 1414 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1415 | static int |
| 1416 | import_from(locals, v, name) |
| 1417 | object *locals; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 1418 | object *v; |
| 1419 | char *name; |
| 1420 | { |
| 1421 | object *w, *x; |
| 1422 | w = getmoduledict(v); |
| 1423 | if (name[0] == '*') { |
| 1424 | int i; |
| 1425 | int n = getdictsize(w); |
| 1426 | for (i = 0; i < n; i++) { |
| 1427 | name = getdictkey(w, i); |
| 1428 | if (name == NULL || name[0] == '_') |
| 1429 | continue; |
| 1430 | x = dictlookup(w, name); |
| 1431 | if (x == NULL) { |
| 1432 | /* XXX can't happen? */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1433 | err_setstr(NameError, name); |
| 1434 | return -1; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 1435 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1436 | if (dictinsert(locals, name, x) != 0) |
| 1437 | return -1; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 1438 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1439 | return 0; |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 1440 | } |
| 1441 | else { |
| 1442 | x = dictlookup(w, name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1443 | if (x == NULL) { |
| 1444 | err_setstr(NameError, name); |
| 1445 | return -1; |
| 1446 | } |
| 1447 | else |
| 1448 | return dictinsert(locals, name, x); |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 1449 | } |
| 1450 | } |
| 1451 | |
| 1452 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1453 | build_class(v, w) |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 1454 | object *v; /* None or tuple containing base classes */ |
| 1455 | object *w; /* dictionary */ |
| 1456 | { |
| 1457 | if (is_tupleobject(v)) { |
| 1458 | int i; |
| 1459 | for (i = gettuplesize(v); --i >= 0; ) { |
| 1460 | object *x = gettupleitem(v, i); |
| 1461 | if (!is_classobject(x)) { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1462 | err_setstr(TypeError, |
| 1463 | "base is not a class object"); |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 1464 | return NULL; |
| 1465 | } |
| 1466 | } |
| 1467 | } |
| 1468 | else { |
| 1469 | v = NULL; |
| 1470 | } |
| 1471 | if (!is_dictobject(w)) { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1472 | err_setstr(SystemError, "build_class with non-dictionary"); |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 1473 | return NULL; |
| 1474 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1475 | return newclassobject(v, w); |
Guido van Rossum | e9736fc | 1990-11-18 17:33:06 +0000 | [diff] [blame] | 1476 | } |