blob: badfcedd77d11801fa811a4cb52711c79f27b4da [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
2Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum3f5da241990-12-20 15:06:42 +000025/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000026
Guido van Rossum3f5da241990-12-20 15:06:42 +000027#include "allobjects.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000028
Guido van Rossum10dc2e81990-11-18 17:27:39 +000029#include "import.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000030#include "sysmodule.h"
31#include "compile.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000032#include "frameobject.h"
33#include "ceval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000034#include "opcode.h"
Guido van Rossum86cd6e61991-01-21 15:12:35 +000035#include "bltinmodule.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000036#include "traceback.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000037
Guido van Rossum3f5da241990-12-20 15:06:42 +000038#ifndef NDEBUG
39#define TRACE
Guido van Rossum10dc2e81990-11-18 17:27:39 +000040#endif
41
Guido van Rossum374a9221991-04-04 10:40:29 +000042/* Forward declarations */
43/* XXX Need prototypes */
44
45void flushline();
46static object *add();
47static object *sub();
48static object *mul();
49static object *divide();
50static object *rem();
51static object *neg();
52static object *pos();
53static object *not();
54static object *call_builtin();
55static object *call_function();
56static object *apply_subscript();
57static object *loop_subscript();
58static object *apply_slice();
59static object *cmp_outcome();
60static object *build_class();
61static int testbool();
62static int assign_subscript PROTO((object *, object *, object *));
63static int assign_slice PROTO((object *, object *, object *, object *));
64static int import_from PROTO((object *, object *, char *));
65
66
67static frameobject *current_frame;
68
69
70/* Status code for main loop (reason for stack unwind) */
71
72enum 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
83object *
84eval_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 (;;) {
165 static ticker;
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();
269 if (is_classmethodobject(v) || is_funcobject(v))
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();
334 if (is_classmethodobject(v) || is_funcobject(v))
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 Rossum3f5da241990-12-20 15:06:42 +0000929#ifdef TRACE
930static int
931prtrace(v, str)
932 object *v;
933 char *str;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000934{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000935 printf("%s ", str);
936 printobject(v, stdout, 0);
937 printf("\n");
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000938}
Guido van Rossum3f5da241990-12-20 15:06:42 +0000939#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000940
Guido van Rossum3f5da241990-12-20 15:06:42 +0000941object *
942getlocals()
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000943{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000944 if (current_frame == NULL)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000945 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000946 else
947 return current_frame->f_locals;
948}
949
950object *
951getglobals()
952{
953 if (current_frame == NULL)
954 return NULL;
955 else
956 return current_frame->f_globals;
957}
958
959void
960printtraceback(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 Rossum10dc2e81990-11-18 17:27:39 +0000968 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000969}
970
971
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000972void
973flushline()
974{
Guido van Rossum374a9221991-04-04 10:40:29 +0000975 if (softspace(sysget("stdout"), 0))
976 fprintf(sysgetfile("stdout", stdout), "\n");
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000977}
978
Guido van Rossum3f5da241990-12-20 15:06:42 +0000979
980/* Test a value used as condition, e.g., in a for or if statement */
981
982static int
983testbool(v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000984 object *v;
985{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000986 if (is_intobject(v))
987 return getintvalue(v) != 0;
988 if (is_floatobject(v))
989 return getfloatvalue(v) != 0.0;
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;
994 if (v == None)
995 return 0;
996 /* All other objects are 'true' */
997 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000998}
999
1000static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001001add(v, w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001002 object *v, *w;
1003{
1004 if (v->ob_type->tp_as_number != NULL)
1005 v = (*v->ob_type->tp_as_number->nb_add)(v, w);
1006 else if (v->ob_type->tp_as_sequence != NULL)
1007 v = (*v->ob_type->tp_as_sequence->sq_concat)(v, w);
1008 else {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001009 err_setstr(TypeError, "+ not supported by operands");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001010 return NULL;
1011 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001012 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001013}
1014
1015static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001016sub(v, w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001017 object *v, *w;
1018{
1019 if (v->ob_type->tp_as_number != NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001020 return (*v->ob_type->tp_as_number->nb_subtract)(v, w);
1021 err_setstr(TypeError, "bad operand type(s) for -");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001022 return NULL;
1023}
1024
1025static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001026mul(v, w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001027 object *v, *w;
1028{
1029 typeobject *tp;
1030 if (is_intobject(v) && w->ob_type->tp_as_sequence != NULL) {
1031 /* int*sequence -- swap v and w */
1032 object *tmp = v;
1033 v = w;
1034 w = tmp;
1035 }
1036 tp = v->ob_type;
1037 if (tp->tp_as_number != NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001038 return (*tp->tp_as_number->nb_multiply)(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001039 if (tp->tp_as_sequence != NULL) {
1040 if (!is_intobject(w)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001041 err_setstr(TypeError,
1042 "can't multiply sequence with non-int");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001043 return NULL;
1044 }
1045 if (tp->tp_as_sequence->sq_repeat == NULL) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001046 err_setstr(TypeError, "sequence does not support *");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001047 return NULL;
1048 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001049 return (*tp->tp_as_sequence->sq_repeat)
1050 (v, (int)getintvalue(w));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001051 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001052 err_setstr(TypeError, "bad operand type(s) for *");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001053 return NULL;
1054}
1055
1056static object *
Guido van Rossum40d0b7e1990-12-20 23:03:11 +00001057divide(v, w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001058 object *v, *w;
1059{
1060 if (v->ob_type->tp_as_number != NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001061 return (*v->ob_type->tp_as_number->nb_divide)(v, w);
1062 err_setstr(TypeError, "bad operand type(s) for /");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001063 return NULL;
1064}
1065
1066static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001067rem(v, w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001068 object *v, *w;
1069{
1070 if (v->ob_type->tp_as_number != NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001071 return (*v->ob_type->tp_as_number->nb_remainder)(v, w);
1072 err_setstr(TypeError, "bad operand type(s) for %");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001073 return NULL;
1074}
1075
1076static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001077neg(v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001078 object *v;
1079{
1080 if (v->ob_type->tp_as_number != NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001081 return (*v->ob_type->tp_as_number->nb_negative)(v);
1082 err_setstr(TypeError, "bad operand type(s) for unary -");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001083 return NULL;
1084}
1085
1086static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001087pos(v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001088 object *v;
1089{
1090 if (v->ob_type->tp_as_number != NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001091 return (*v->ob_type->tp_as_number->nb_positive)(v);
1092 err_setstr(TypeError, "bad operand type(s) for unary +");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001093 return NULL;
1094}
1095
1096static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001097not(v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001098 object *v;
1099{
Guido van Rossum3f5da241990-12-20 15:06:42 +00001100 int outcome = testbool(v);
1101 object *w = outcome == 0 ? True : False;
1102 INCREF(w);
1103 return w;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001104}
1105
1106static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001107call_builtin(func, arg)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001108 object *func;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001109 object *arg;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001110{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001111 if (is_methodobject(func)) {
1112 method meth = getmethod(func);
1113 object *self = getself(func);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001114 return (*meth)(self, arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001115 }
1116 if (is_classobject(func)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001117 if (arg != NULL) {
1118 err_setstr(TypeError,
1119 "classobject() allows no arguments");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001120 return NULL;
1121 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001122 return newinstanceobject(func);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001123 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001124 err_setstr(TypeError, "call of non-function");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001125 return NULL;
1126}
1127
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001128static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001129call_function(func, arg)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001130 object *func;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001131 object *arg;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001132{
Guido van Rossum3f5da241990-12-20 15:06:42 +00001133 object *newarg = NULL;
1134 object *newlocals, *newglobals;
1135 object *co, *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001136
1137 if (is_classmethodobject(func)) {
1138 object *self = classmethodgetself(func);
1139 func = classmethodgetfunc(func);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001140 if (arg == NULL) {
1141 arg = self;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001142 }
1143 else {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001144 newarg = newtupleobject(2);
1145 if (newarg == NULL)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001146 return NULL;
1147 INCREF(self);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001148 INCREF(arg);
1149 settupleitem(newarg, 0, self);
1150 settupleitem(newarg, 1, arg);
1151 arg = newarg;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001152 }
1153 }
1154 else {
1155 if (!is_funcobject(func)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001156 err_setstr(TypeError, "call of non-function");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001157 return NULL;
1158 }
1159 }
1160
Guido van Rossum3f5da241990-12-20 15:06:42 +00001161 co = getfunccode(func);
1162 if (co == NULL) {
1163 XDECREF(newarg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001164 return NULL;
1165 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001166 if (!is_codeobject(co)) {
1167 fprintf(stderr, "XXX Bad code\n");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001168 abort();
1169 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001170 newlocals = newdictobject();
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001171 if (newlocals == NULL) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001172 XDECREF(newarg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001173 return NULL;
1174 }
1175
Guido van Rossum3f5da241990-12-20 15:06:42 +00001176 newglobals = getfuncglobals(func);
1177 INCREF(newglobals);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001178
Guido van Rossum3f5da241990-12-20 15:06:42 +00001179 v = eval_code((codeobject *)co, newglobals, newlocals, arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001180
Guido van Rossum3f5da241990-12-20 15:06:42 +00001181 DECREF(newlocals);
1182 DECREF(newglobals);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001183
Guido van Rossum3f5da241990-12-20 15:06:42 +00001184 XDECREF(newarg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001185
1186 return v;
1187}
1188
1189static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001190apply_subscript(v, w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001191 object *v, *w;
1192{
1193 typeobject *tp = v->ob_type;
1194 if (tp->tp_as_sequence == NULL && tp->tp_as_mapping == NULL) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001195 err_setstr(TypeError, "unsubscriptable object");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001196 return NULL;
1197 }
1198 if (tp->tp_as_sequence != NULL) {
1199 int i;
1200 if (!is_intobject(w)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001201 err_setstr(TypeError, "sequence subscript not int");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001202 return NULL;
1203 }
1204 i = getintvalue(w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001205 return (*tp->tp_as_sequence->sq_item)(v, i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001206 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001207 return (*tp->tp_as_mapping->mp_subscript)(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001208}
1209
1210static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001211loop_subscript(v, w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001212 object *v, *w;
1213{
1214 sequence_methods *sq = v->ob_type->tp_as_sequence;
1215 int i, n;
1216 if (sq == NULL) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001217 err_setstr(TypeError, "loop over non-sequence");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001218 return NULL;
1219 }
1220 i = getintvalue(w);
1221 n = (*sq->sq_length)(v);
1222 if (i >= n)
1223 return NULL; /* End of loop */
Guido van Rossum3f5da241990-12-20 15:06:42 +00001224 return (*sq->sq_item)(v, i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001225}
1226
1227static int
Guido van Rossum3f5da241990-12-20 15:06:42 +00001228slice_index(v, isize, pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001229 object *v;
1230 int isize;
1231 int *pi;
1232{
1233 if (v != NULL) {
1234 if (!is_intobject(v)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001235 err_setstr(TypeError, "slice index must be int");
1236 return -1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001237 }
1238 *pi = getintvalue(v);
1239 if (*pi < 0)
1240 *pi += isize;
1241 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001242 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001243}
1244
1245static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001246apply_slice(u, v, w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001247 object *u, *v, *w;
1248{
1249 typeobject *tp = u->ob_type;
1250 int ilow, ihigh, isize;
1251 if (tp->tp_as_sequence == NULL) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001252 err_setstr(TypeError, "only sequences can be sliced");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001253 return NULL;
1254 }
1255 ilow = 0;
1256 isize = ihigh = (*tp->tp_as_sequence->sq_length)(u);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001257 if (slice_index(v, isize, &ilow) != 0)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001258 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001259 if (slice_index(w, isize, &ihigh) != 0)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001260 return NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001261 return (*tp->tp_as_sequence->sq_slice)(u, ilow, ihigh);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001262}
Guido van Rossum3f5da241990-12-20 15:06:42 +00001263
1264static int
1265assign_subscript(w, key, v) /* w[key] = v */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001266 object *w;
1267 object *key;
1268 object *v;
1269{
1270 typeobject *tp = w->ob_type;
1271 sequence_methods *sq;
1272 mapping_methods *mp;
1273 int (*func)();
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001274 if ((sq = tp->tp_as_sequence) != NULL &&
1275 (func = sq->sq_ass_item) != NULL) {
1276 if (!is_intobject(key)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001277 err_setstr(TypeError,
1278 "sequence subscript must be integer");
1279 return -1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001280 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001281 else
1282 return (*func)(w, (int)getintvalue(key), v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001283 }
1284 else if ((mp = tp->tp_as_mapping) != NULL &&
1285 (func = mp->mp_ass_subscript) != NULL) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001286 return (*func)(w, key, v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001287 }
1288 else {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001289 err_setstr(TypeError,
1290 "can't assign to this subscripted object");
1291 return -1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001292 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001293}
1294
Guido van Rossum3f5da241990-12-20 15:06:42 +00001295static int
1296assign_slice(u, v, w, x) /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001297 object *u, *v, *w, *x;
1298{
Guido van Rossum3f5da241990-12-20 15:06:42 +00001299 sequence_methods *sq = u->ob_type->tp_as_sequence;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001300 int ilow, ihigh, isize;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001301 if (sq == NULL) {
1302 err_setstr(TypeError, "assign to slice of non-sequence");
1303 return -1;
1304 }
1305 if (sq == NULL || sq->sq_ass_slice == NULL) {
1306 err_setstr(TypeError, "unassignable slice");
1307 return -1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001308 }
1309 ilow = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001310 isize = ihigh = (*sq->sq_length)(u);
1311 if (slice_index(v, isize, &ilow) != 0)
1312 return -1;
1313 if (slice_index(w, isize, &ihigh) != 0)
1314 return -1;
1315 return (*sq->sq_ass_slice)(u, ilow, ihigh, x);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001316}
1317
1318static int
1319cmp_exception(err, v)
1320 object *err, *v;
1321{
1322 if (is_tupleobject(v)) {
1323 int i, n;
1324 n = gettuplesize(v);
1325 for (i = 0; i < n; i++) {
1326 if (err == gettupleitem(v, i))
1327 return 1;
1328 }
1329 return 0;
1330 }
1331 return err == v;
1332}
1333
Guido van Rossum3f5da241990-12-20 15:06:42 +00001334static int
1335cmp_member(v, w)
1336 object *v, *w;
1337{
1338 int i, n, cmp;
1339 object *x;
1340 sequence_methods *sq;
1341 /* Special case for char in string */
1342 if (is_stringobject(w)) {
1343 register char *s, *end;
1344 register char c;
1345 if (!is_stringobject(v) || getstringsize(v) != 1) {
1346 err_setstr(TypeError,
1347 "string member test needs char left operand");
1348 return -1;
1349 }
1350 c = getstringvalue(v)[0];
1351 s = getstringvalue(w);
1352 end = s + getstringsize(w);
1353 while (s < end) {
1354 if (c == *s++)
1355 return 1;
1356 }
1357 return 0;
1358 }
1359 sq = w->ob_type->tp_as_sequence;
1360 if (sq == NULL) {
1361 err_setstr(TypeError,
1362 "'in' or 'not in' needs sequence right argument");
1363 return -1;
1364 }
1365 n = (*sq->sq_length)(w);
1366 for (i = 0; i < n; i++) {
1367 x = (*sq->sq_item)(w, i);
1368 cmp = cmpobject(v, x);
1369 XDECREF(x);
1370 if (cmp == 0)
1371 return 1;
1372 }
1373 return 0;
1374}
1375
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001376static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001377cmp_outcome(op, v, w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001378 enum cmp_op op;
1379 register object *v;
1380 register object *w;
1381{
1382 register int cmp;
1383 register int res = 0;
1384 switch (op) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001385 case IS:
1386 case IS_NOT:
1387 res = (v == w);
1388 if (op == IS_NOT)
1389 res = !res;
1390 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001391 case IN:
1392 case NOT_IN:
Guido van Rossum3f5da241990-12-20 15:06:42 +00001393 res = cmp_member(v, w);
1394 if (res < 0)
1395 return NULL;
1396 if (op == NOT_IN)
1397 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001398 break;
1399 case EXC_MATCH:
Guido van Rossum3f5da241990-12-20 15:06:42 +00001400 res = cmp_exception(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001401 break;
1402 default:
Guido van Rossum3f5da241990-12-20 15:06:42 +00001403 cmp = cmpobject(v, w);
1404 switch (op) {
1405 case LT: res = cmp < 0; break;
1406 case LE: res = cmp <= 0; break;
1407 case EQ: res = cmp == 0; break;
1408 case NE: res = cmp != 0; break;
1409 case GT: res = cmp > 0; break;
1410 case GE: res = cmp >= 0; break;
1411 /* XXX no default? (res is initialized to 0 though) */
1412 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001413 }
1414 v = res ? True : False;
1415 INCREF(v);
1416 return v;
1417}
1418
Guido van Rossum374a9221991-04-04 10:40:29 +00001419/* XXX This function should use dict2 variants (change interface!) */
1420
Guido van Rossum3f5da241990-12-20 15:06:42 +00001421static int
1422import_from(locals, v, name)
1423 object *locals;
Guido van Rossume9736fc1990-11-18 17:33:06 +00001424 object *v;
1425 char *name;
1426{
1427 object *w, *x;
1428 w = getmoduledict(v);
1429 if (name[0] == '*') {
1430 int i;
1431 int n = getdictsize(w);
1432 for (i = 0; i < n; i++) {
1433 name = getdictkey(w, i);
1434 if (name == NULL || name[0] == '_')
1435 continue;
1436 x = dictlookup(w, name);
1437 if (x == NULL) {
1438 /* XXX can't happen? */
Guido van Rossum3f5da241990-12-20 15:06:42 +00001439 err_setstr(NameError, name);
1440 return -1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00001441 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001442 if (dictinsert(locals, name, x) != 0)
1443 return -1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00001444 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001445 return 0;
Guido van Rossume9736fc1990-11-18 17:33:06 +00001446 }
1447 else {
1448 x = dictlookup(w, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001449 if (x == NULL) {
1450 err_setstr(NameError, name);
1451 return -1;
1452 }
1453 else
1454 return dictinsert(locals, name, x);
Guido van Rossume9736fc1990-11-18 17:33:06 +00001455 }
1456}
1457
1458static object *
Guido van Rossum3f5da241990-12-20 15:06:42 +00001459build_class(v, w)
Guido van Rossume9736fc1990-11-18 17:33:06 +00001460 object *v; /* None or tuple containing base classes */
1461 object *w; /* dictionary */
1462{
1463 if (is_tupleobject(v)) {
1464 int i;
1465 for (i = gettuplesize(v); --i >= 0; ) {
1466 object *x = gettupleitem(v, i);
1467 if (!is_classobject(x)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001468 err_setstr(TypeError,
1469 "base is not a class object");
Guido van Rossume9736fc1990-11-18 17:33:06 +00001470 return NULL;
1471 }
1472 }
1473 }
1474 else {
1475 v = NULL;
1476 }
1477 if (!is_dictobject(w)) {
Guido van Rossum3f5da241990-12-20 15:06:42 +00001478 err_setstr(SystemError, "build_class with non-dictionary");
Guido van Rossume9736fc1990-11-18 17:33:06 +00001479 return NULL;
1480 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00001481 return newclassobject(v, w);
Guido van Rossume9736fc1990-11-18 17:33:06 +00001482}