-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathceval.c
2877 lines (2623 loc) · 62.7 KB
/
ceval.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***********************************************************
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
The Netherlands.
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the names of Stichting Mathematisch
Centrum or CWI or Corporation for National Research Initiatives or
CNRI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.
While CWI is the initial source for this software, a modified version
is made available by the Corporation for National Research Initiatives
(CNRI) at the Internet address ftp://ftp.python.org.
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
******************************************************************/
/* Execute compiled code */
/* XXX TO DO:
XXX how to pass arguments to call_trace?
XXX speed up searching for keywords by using a dictionary
XXX document it!
*/
#include "Python.h"
#include "compile.h"
#include "frameobject.h"
#include "eval.h"
#include "opcode.h"
#include <ctype.h>
#ifdef HAVE_LIMITS_H
#include <limits.h>
#else
#define INT_MAX 2147483647
#endif
/* Turn this on if your compiler chokes on the big switch: */
/* #define CASE_TOO_BIG 1 */
#ifdef Py_DEBUG
/* For debugging the interpreter: */
#define LLTRACE 1 /* Low-level trace feature */
#define CHECKEXC 1 /* Double-check exception checking */
#endif
/* Forward declarations */
static PyObject *eval_code2 Py_PROTO((PyCodeObject *,
PyObject *, PyObject *,
PyObject **, int,
PyObject **, int,
PyObject **, int,
PyObject *));
#ifdef LLTRACE
static int prtrace Py_PROTO((PyObject *, char *));
#endif
static void call_exc_trace Py_PROTO((PyObject **, PyObject**,
PyFrameObject *));
static int call_trace Py_PROTO((PyObject **, PyObject **,
PyFrameObject *, char *, PyObject *));
static PyObject *call_builtin Py_PROTO((PyObject *, PyObject *, PyObject *));
static PyObject *call_function Py_PROTO((PyObject *, PyObject *, PyObject *));
static PyObject *loop_subscript Py_PROTO((PyObject *, PyObject *));
static int slice_index Py_PROTO((PyObject *, int *));
static PyObject *apply_slice Py_PROTO((PyObject *, PyObject *, PyObject *));
static int assign_slice Py_PROTO((PyObject *, PyObject *,
PyObject *, PyObject *));
static PyObject *cmp_outcome Py_PROTO((int, PyObject *, PyObject *));
static int import_from Py_PROTO((PyObject *, PyObject *, PyObject *));
static PyObject *build_class Py_PROTO((PyObject *, PyObject *, PyObject *));
static int exec_statement Py_PROTO((PyFrameObject *,
PyObject *, PyObject *, PyObject *));
static PyObject *find_from_args Py_PROTO((PyFrameObject *, int));
static void set_exc_info Py_PROTO((PyThreadState *,
PyObject *, PyObject *, PyObject *));
static void reset_exc_info Py_PROTO((PyThreadState *));
/* Dynamic execution profile */
#ifdef DYNAMIC_EXECUTION_PROFILE
#ifdef DXPAIRS
static long dxpairs[257][256];
#define dxp dxpairs[256]
#else
static long dxp[256];
#endif
#endif
#ifdef WITH_THREAD
#include <errno.h>
#include "pythread.h"
extern int _PyThread_Started; /* Flag for Py_Exit */
static PyThread_type_lock interpreter_lock = 0;
static long main_thread = 0;
void
PyEval_InitThreads()
{
if (interpreter_lock)
return;
_PyThread_Started = 1;
interpreter_lock = PyThread_allocate_lock();
PyThread_acquire_lock(interpreter_lock, 1);
main_thread = PyThread_get_thread_ident();
}
void
PyEval_AcquireLock()
{
PyThread_acquire_lock(interpreter_lock, 1);
}
void
PyEval_ReleaseLock()
{
PyThread_release_lock(interpreter_lock);
}
void
PyEval_AcquireThread(tstate)
PyThreadState *tstate;
{
if (tstate == NULL)
Py_FatalError("PyEval_AcquireThread: NULL new thread state");
PyThread_acquire_lock(interpreter_lock, 1);
if (PyThreadState_Swap(tstate) != NULL)
Py_FatalError(
"PyEval_AcquireThread: non-NULL old thread state");
}
void
PyEval_ReleaseThread(tstate)
PyThreadState *tstate;
{
if (tstate == NULL)
Py_FatalError("PyEval_ReleaseThread: NULL thread state");
if (PyThreadState_Swap(NULL) != tstate)
Py_FatalError("PyEval_ReleaseThread: wrong thread state");
PyThread_release_lock(interpreter_lock);
}
#endif
/* Functions save_thread and restore_thread are always defined so
dynamically loaded modules needn't be compiled separately for use
with and without threads: */
PyThreadState *
PyEval_SaveThread()
{
PyThreadState *tstate = PyThreadState_Swap(NULL);
if (tstate == NULL)
Py_FatalError("PyEval_SaveThread: NULL tstate");
#ifdef WITH_THREAD
if (interpreter_lock)
PyThread_release_lock(interpreter_lock);
#endif
return tstate;
}
void
PyEval_RestoreThread(tstate)
PyThreadState *tstate;
{
if (tstate == NULL)
Py_FatalError("PyEval_RestoreThread: NULL tstate");
#ifdef WITH_THREAD
if (interpreter_lock) {
int err = errno;
PyThread_acquire_lock(interpreter_lock, 1);
errno = err;
}
#endif
PyThreadState_Swap(tstate);
}
/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
signal handlers or Mac I/O completion routines) can schedule calls
to a function to be called synchronously.
The synchronous function is called with one void* argument.
It should return 0 for success or -1 for failure -- failure should
be accompanied by an exception.
If registry succeeds, the registry function returns 0; if it fails
(e.g. due to too many pending calls) it returns -1 (without setting
an exception condition).
Note that because registry may occur from within signal handlers,
or other asynchronous events, calling malloc() is unsafe!
#ifdef WITH_THREAD
Any thread can schedule pending calls, but only the main thread
will execute them.
#endif
XXX WARNING! ASYNCHRONOUSLY EXECUTING CODE!
There are two possible race conditions:
(1) nested asynchronous registry calls;
(2) registry calls made while pending calls are being processed.
While (1) is very unlikely, (2) is a real possibility.
The current code is safe against (2), but not against (1).
The safety against (2) is derived from the fact that only one
thread (the main thread) ever takes things out of the queue.
XXX Darn! With the advent of thread state, we should have an array
of pending calls per thread in the thread state! Later...
*/
#define NPENDINGCALLS 32
static struct {
int (*func) Py_PROTO((ANY *));
ANY *arg;
} pendingcalls[NPENDINGCALLS];
static volatile int pendingfirst = 0;
static volatile int pendinglast = 0;
static volatile int things_to_do = 0;
int
Py_AddPendingCall(func, arg)
int (*func) Py_PROTO((ANY *));
ANY *arg;
{
static int busy = 0;
int i, j;
/* XXX Begin critical section */
/* XXX If you want this to be safe against nested
XXX asynchronous calls, you'll have to work harder! */
if (busy)
return -1;
busy = 1;
i = pendinglast;
j = (i + 1) % NPENDINGCALLS;
if (j == pendingfirst)
return -1; /* Queue full */
pendingcalls[i].func = func;
pendingcalls[i].arg = arg;
pendinglast = j;
things_to_do = 1; /* Signal main loop */
busy = 0;
/* XXX End critical section */
return 0;
}
int
Py_MakePendingCalls()
{
static int busy = 0;
#ifdef WITH_THREAD
if (main_thread && PyThread_get_thread_ident() != main_thread)
return 0;
#endif
if (busy)
return 0;
busy = 1;
things_to_do = 0;
for (;;) {
int i;
int (*func) Py_PROTO((ANY *));
ANY *arg;
i = pendingfirst;
if (i == pendinglast)
break; /* Queue empty */
func = pendingcalls[i].func;
arg = pendingcalls[i].arg;
pendingfirst = (i + 1) % NPENDINGCALLS;
if (func(arg) < 0) {
busy = 0;
things_to_do = 1; /* We're not done yet */
return -1;
}
}
busy = 0;
return 0;
}
/* Status code for main loop (reason for stack unwind) */
enum why_code {
WHY_NOT, /* No error */
WHY_EXCEPTION, /* Exception occurred */
WHY_RERAISE, /* Exception re-raised by 'finally' */
WHY_RETURN, /* 'return' statement */
WHY_BREAK /* 'break' statement */
};
static enum why_code do_raise Py_PROTO((PyObject *, PyObject *, PyObject *));
static int unpack_sequence Py_PROTO((PyObject *, int, PyObject **));
/* Backward compatible interface */
PyObject *
PyEval_EvalCode(co, globals, locals)
PyCodeObject *co;
PyObject *globals;
PyObject *locals;
{
return eval_code2(co,
globals, locals,
(PyObject **)NULL, 0,
(PyObject **)NULL, 0,
(PyObject **)NULL, 0,
(PyObject *)NULL);
}
/* Interpreter main loop */
#ifndef MAX_RECURSION_DEPTH
#define MAX_RECURSION_DEPTH 10000
#endif
static PyObject *
eval_code2(co, globals, locals,
args, argcount, kws, kwcount, defs, defcount, owner)
PyCodeObject *co;
PyObject *globals;
PyObject *locals;
PyObject **args;
int argcount;
PyObject **kws; /* length: 2*kwcount */
int kwcount;
PyObject **defs;
int defcount;
PyObject *owner;
{
#ifdef DXPAIRS
int lastopcode = 0;
#endif
register unsigned char *next_instr;
register int opcode; /* Current opcode */
register int oparg; /* Current opcode argument, if any */
register PyObject **stack_pointer;
register enum why_code why; /* Reason for block stack unwind */
register int err; /* Error status -- nonzero if error */
register PyObject *x; /* Result object -- NULL if error */
register PyObject *v; /* Temporary objects popped off stack */
register PyObject *w;
register PyObject *u;
register PyObject *t;
register PyFrameObject *f; /* Current frame */
register PyObject **fastlocals;
PyObject *retval = NULL; /* Return value */
PyThreadState *tstate = PyThreadState_GET();
unsigned char *first_instr;
#ifdef LLTRACE
int lltrace;
#endif
#if defined(Py_DEBUG) || defined(LLTRACE)
/* Make it easier to find out where we are with a debugger */
char *filename = PyString_AsString(co->co_filename);
#endif
/* Code access macros */
#define GETCONST(i) Getconst(f, i)
#define GETNAME(i) Getname(f, i)
#define GETNAMEV(i) Getnamev(f, i)
#define INSTR_OFFSET() (next_instr - first_instr)
#define NEXTOP() (*next_instr++)
#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
#define JUMPTO(x) (next_instr = first_instr + (x))
#define JUMPBY(x) (next_instr += (x))
/* Stack manipulation macros */
#define STACK_LEVEL() (stack_pointer - f->f_valuestack)
#define EMPTY() (STACK_LEVEL() == 0)
#define TOP() (stack_pointer[-1])
#define BASIC_PUSH(v) (*stack_pointer++ = (v))
#define BASIC_POP() (*--stack_pointer)
#ifdef LLTRACE
#define PUSH(v) (BASIC_PUSH(v), lltrace && prtrace(TOP(), "push"))
#define POP() (lltrace && prtrace(TOP(), "pop"), BASIC_POP())
#else
#define PUSH(v) BASIC_PUSH(v)
#define POP() BASIC_POP()
#endif
/* Local variable macros */
#define GETLOCAL(i) (fastlocals[i])
#define SETLOCAL(i, value) do { Py_XDECREF(GETLOCAL(i)); \
GETLOCAL(i) = value; } while (0)
/* Start of code */
#ifdef USE_STACKCHECK
if (tstate->recursion_depth%10 == 0 && PyOS_CheckStack()) {
PyErr_SetString(PyExc_MemoryError, "Stack overflow");
return NULL;
}
#endif
if (globals == NULL) {
PyErr_SetString(PyExc_SystemError, "eval_code2: NULL globals");
return NULL;
}
#ifdef LLTRACE
lltrace = PyDict_GetItemString(globals, "__lltrace__") != NULL;
#endif
f = PyFrame_New(
tstate, /*back*/
co, /*code*/
globals, /*globals*/
locals); /*locals*/
if (f == NULL)
return NULL;
tstate->frame = f;
fastlocals = f->f_localsplus;
if (co->co_argcount > 0 ||
co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
int i;
int n = argcount;
PyObject *kwdict = NULL;
if (co->co_flags & CO_VARKEYWORDS) {
kwdict = PyDict_New();
if (kwdict == NULL)
goto fail;
i = co->co_argcount;
if (co->co_flags & CO_VARARGS)
i++;
SETLOCAL(i, kwdict);
}
if (argcount > co->co_argcount) {
if (!(co->co_flags & CO_VARARGS)) {
PyErr_Format(PyExc_TypeError,
"too many arguments; expected %d, got %d",
co->co_argcount, argcount);
goto fail;
}
n = co->co_argcount;
}
for (i = 0; i < n; i++) {
x = args[i];
Py_INCREF(x);
SETLOCAL(i, x);
}
if (co->co_flags & CO_VARARGS) {
u = PyTuple_New(argcount - n);
if (u == NULL)
goto fail;
SETLOCAL(co->co_argcount, u);
for (i = n; i < argcount; i++) {
x = args[i];
Py_INCREF(x);
PyTuple_SET_ITEM(u, i-n, x);
}
}
for (i = 0; i < kwcount; i++) {
PyObject *keyword = kws[2*i];
PyObject *value = kws[2*i + 1];
int j;
/* XXX slow -- speed up using dictionary? */
for (j = 0; j < co->co_argcount; j++) {
PyObject *nm = PyTuple_GET_ITEM(
co->co_varnames, j);
if (PyObject_Compare(keyword, nm) == 0)
break;
}
/* Check errors from Compare */
if (PyErr_Occurred())
goto fail;
if (j >= co->co_argcount) {
if (kwdict == NULL) {
PyErr_Format(PyExc_TypeError,
"unexpected keyword argument: %.400s",
PyString_AsString(keyword));
goto fail;
}
PyDict_SetItem(kwdict, keyword, value);
}
else {
if (GETLOCAL(j) != NULL) {
PyErr_SetString(PyExc_TypeError,
"keyword parameter redefined");
goto fail;
}
Py_INCREF(value);
SETLOCAL(j, value);
}
}
if (argcount < co->co_argcount) {
int m = co->co_argcount - defcount;
for (i = argcount; i < m; i++) {
if (GETLOCAL(i) == NULL) {
PyErr_Format(PyExc_TypeError,
"not enough arguments; expected %d, got %d",
m, i);
goto fail;
}
}
if (n > m)
i = n - m;
else
i = 0;
for (; i < defcount; i++) {
if (GETLOCAL(m+i) == NULL) {
PyObject *def = defs[i];
Py_INCREF(def);
SETLOCAL(m+i, def);
}
}
}
}
else {
if (argcount > 0 || kwcount > 0) {
PyErr_SetString(PyExc_TypeError,
"no arguments expected");
goto fail;
}
}
if (tstate->sys_tracefunc != NULL) {
/* tstate->sys_tracefunc, if defined, is a function that
will be called on *every* entry to a code block.
Its return value, if not None, is a function that
will be called at the start of each executed line
of code. (Actually, the function must return
itself in order to continue tracing.)
The trace functions are called with three arguments:
a pointer to the current frame, a string indicating
why the function is called, and an argument which
depends on the situation. The global trace function
(sys.trace) is also called whenever an exception
is detected. */
if (call_trace(&tstate->sys_tracefunc,
&f->f_trace, f, "call",
Py_None/*XXX how to compute arguments now?*/)) {
/* Trace function raised an error */
goto fail;
}
}
if (tstate->sys_profilefunc != NULL) {
/* Similar for sys_profilefunc, except it needn't return
itself and isn't called for "line" events */
if (call_trace(&tstate->sys_profilefunc,
(PyObject**)0, f, "call",
Py_None/*XXX*/)) {
goto fail;
}
}
if (++tstate->recursion_depth > MAX_RECURSION_DEPTH) {
--tstate->recursion_depth;
PyErr_SetString(PyExc_RuntimeError,
"Maximum recursion depth exceeded");
tstate->frame = f->f_back;
Py_DECREF(f);
return NULL;
}
_PyCode_GETCODEPTR(co, &first_instr);
next_instr = first_instr;
stack_pointer = f->f_valuestack;
why = WHY_NOT;
err = 0;
x = Py_None; /* Not a reference, just anything non-NULL */
for (;;) {
/* Do periodic things. Doing this every time through
the loop would add too much overhead, so we do it
only every Nth instruction. We also do it if
``things_to_do'' is set, i.e. when an asynchronous
event needs attention (e.g. a signal handler or
async I/O handler); see Py_AddPendingCall() and
Py_MakePendingCalls() above. */
if (things_to_do || --tstate->ticker < 0) {
tstate->ticker = tstate->interp->checkinterval;
if (things_to_do) {
if (Py_MakePendingCalls() < 0) {
why = WHY_EXCEPTION;
goto on_error;
}
}
#if !defined(HAVE_SIGNAL_H) || defined(macintosh)
/* If we have true signals, the signal handler
will call Py_AddPendingCall() so we don't
have to call sigcheck(). On the Mac and
DOS, alas, we have to call it. */
if (PyErr_CheckSignals()) {
why = WHY_EXCEPTION;
goto on_error;
}
#endif
#ifdef WITH_THREAD
if (interpreter_lock) {
/* Give another thread a chance */
if (PyThreadState_Swap(NULL) != tstate)
Py_FatalError("ceval: tstate mix-up");
PyThread_release_lock(interpreter_lock);
/* Other threads may run now */
PyThread_acquire_lock(interpreter_lock, 1);
if (PyThreadState_Swap(tstate) != NULL)
Py_FatalError("ceval: orphan tstate");
}
#endif
}
/* Extract opcode and argument */
#if defined(Py_DEBUG) || defined(LLTRACE)
f->f_lasti = INSTR_OFFSET();
#endif
opcode = NEXTOP();
if (HAS_ARG(opcode))
oparg = NEXTARG();
#ifdef DYNAMIC_EXECUTION_PROFILE
#ifdef DXPAIRS
dxpairs[lastopcode][opcode]++;
lastopcode = opcode;
#endif
dxp[opcode]++;
#endif
#ifdef LLTRACE
/* Instruction tracing */
if (lltrace) {
if (HAS_ARG(opcode)) {
printf("%d: %d, %d\n",
(int) (INSTR_OFFSET() - 3),
opcode, oparg);
}
else {
printf("%d: %d\n",
(int) (INSTR_OFFSET() - 1), opcode);
}
}
#endif
/* Main switch on opcode */
switch (opcode) {
/* BEWARE!
It is essential that any operation that fails sets either
x to NULL, err to nonzero, or why to anything but WHY_NOT,
and that no operation that succeeds does this! */
/* case STOP_CODE: this is an error! */
case POP_TOP:
v = POP();
Py_DECREF(v);
continue;
case ROT_TWO:
v = POP();
w = POP();
PUSH(v);
PUSH(w);
continue;
case ROT_THREE:
v = POP();
w = POP();
x = POP();
PUSH(v);
PUSH(x);
PUSH(w);
continue;
case DUP_TOP:
v = TOP();
Py_INCREF(v);
PUSH(v);
continue;
case UNARY_POSITIVE:
v = POP();
x = PyNumber_Positive(v);
Py_DECREF(v);
PUSH(x);
if (x != NULL) continue;
break;
case UNARY_NEGATIVE:
v = POP();
x = PyNumber_Negative(v);
Py_DECREF(v);
PUSH(x);
if (x != NULL) continue;
break;
case UNARY_NOT:
v = POP();
err = PyObject_IsTrue(v);
Py_DECREF(v);
if (err == 0) {
Py_INCREF(Py_True);
PUSH(Py_True);
continue;
}
else if (err > 0) {
Py_INCREF(Py_False);
PUSH(Py_False);
err = 0;
continue;
}
break;
case UNARY_CONVERT:
v = POP();
x = PyObject_Repr(v);
Py_DECREF(v);
PUSH(x);
if (x != NULL) continue;
break;
case UNARY_INVERT:
v = POP();
x = PyNumber_Invert(v);
Py_DECREF(v);
PUSH(x);
if (x != NULL) continue;
break;
case BINARY_POWER:
w = POP();
v = POP();
x = PyNumber_Power(v, w, Py_None);
Py_DECREF(v);
Py_DECREF(w);
PUSH(x);
if (x != NULL) continue;
break;
case BINARY_MULTIPLY:
w = POP();
v = POP();
x = PyNumber_Multiply(v, w);
Py_DECREF(v);
Py_DECREF(w);
PUSH(x);
if (x != NULL) continue;
break;
case BINARY_DIVIDE:
w = POP();
v = POP();
x = PyNumber_Divide(v, w);
Py_DECREF(v);
Py_DECREF(w);
PUSH(x);
if (x != NULL) continue;
break;
case BINARY_MODULO:
w = POP();
v = POP();
x = PyNumber_Remainder(v, w);
Py_DECREF(v);
Py_DECREF(w);
PUSH(x);
if (x != NULL) continue;
break;
case BINARY_ADD:
w = POP();
v = POP();
if (PyInt_Check(v) && PyInt_Check(w)) {
/* INLINE: int + int */
register long a, b, i;
a = PyInt_AS_LONG(v);
b = PyInt_AS_LONG(w);
i = a + b;
if ((i^a) < 0 && (i^b) < 0) {
PyErr_SetString(PyExc_OverflowError,
"integer addition");
x = NULL;
}
else
x = PyInt_FromLong(i);
}
else
x = PyNumber_Add(v, w);
Py_DECREF(v);
Py_DECREF(w);
PUSH(x);
if (x != NULL) continue;
break;
case BINARY_SUBTRACT:
w = POP();
v = POP();
if (PyInt_Check(v) && PyInt_Check(w)) {
/* INLINE: int - int */
register long a, b, i;
a = PyInt_AS_LONG(v);
b = PyInt_AS_LONG(w);
i = a - b;
if ((i^a) < 0 && (i^~b) < 0) {
PyErr_SetString(PyExc_OverflowError,
"integer subtraction");
x = NULL;
}
else
x = PyInt_FromLong(i);
}
else
x = PyNumber_Subtract(v, w);
Py_DECREF(v);
Py_DECREF(w);
PUSH(x);
if (x != NULL) continue;
break;
case BINARY_SUBSCR:
w = POP();
v = POP();
if (PyList_Check(v) && PyInt_Check(w)) {
/* INLINE: list[int] */
long i = PyInt_AsLong(w);
if (i < 0)
i += PyList_GET_SIZE(v);
if (i < 0 ||
i >= PyList_GET_SIZE(v)) {
PyErr_SetString(PyExc_IndexError,
"list index out of range");
x = NULL;
}
else {
x = PyList_GET_ITEM(v, i);
Py_INCREF(x);
}
}
else
x = PyObject_GetItem(v, w);
Py_DECREF(v);
Py_DECREF(w);
PUSH(x);
if (x != NULL) continue;
break;
case BINARY_LSHIFT:
w = POP();
v = POP();
x = PyNumber_Lshift(v, w);
Py_DECREF(v);
Py_DECREF(w);
PUSH(x);
if (x != NULL) continue;
break;
case BINARY_RSHIFT:
w = POP();
v = POP();
x = PyNumber_Rshift(v, w);
Py_DECREF(v);
Py_DECREF(w);
PUSH(x);
if (x != NULL) continue;
break;
case BINARY_AND:
w = POP();
v = POP();
x = PyNumber_And(v, w);
Py_DECREF(v);
Py_DECREF(w);
PUSH(x);
if (x != NULL) continue;
break;
case BINARY_XOR:
w = POP();
v = POP();
x = PyNumber_Xor(v, w);
Py_DECREF(v);
Py_DECREF(w);
PUSH(x);
if (x != NULL) continue;
break;
case BINARY_OR:
w = POP();
v = POP();
x = PyNumber_Or(v, w);
Py_DECREF(v);
Py_DECREF(w);
PUSH(x);
if (x != NULL) continue;
break;
case SLICE+0:
case SLICE+1:
case SLICE+2:
case SLICE+3:
if ((opcode-SLICE) & 2)
w = POP();
else
w = NULL;
if ((opcode-SLICE) & 1)
v = POP();
else
v = NULL;
u = POP();
x = apply_slice(u, v, w);
Py_DECREF(u);
Py_XDECREF(v);
Py_XDECREF(w);
PUSH(x);
if (x != NULL) continue;
break;
case STORE_SLICE+0:
case STORE_SLICE+1:
case STORE_SLICE+2:
case STORE_SLICE+3:
if ((opcode-STORE_SLICE) & 2)
w = POP();
else
w = NULL;
if ((opcode-STORE_SLICE) & 1)
v = POP();
else
v = NULL;
u = POP();
t = POP();
err = assign_slice(u, v, w, t); /* u[v:w] = t */
Py_DECREF(t);
Py_DECREF(u);
Py_XDECREF(v);
Py_XDECREF(w);
if (err == 0) continue;
break;
case DELETE_SLICE+0:
case DELETE_SLICE+1:
case DELETE_SLICE+2:
case DELETE_SLICE+3:
if ((opcode-DELETE_SLICE) & 2)
w = POP();
else
w = NULL;
if ((opcode-DELETE_SLICE) & 1)
v = POP();
else
v = NULL;
u = POP();
err = assign_slice(u, v, w, (PyObject *)NULL);
/* del u[v:w] */
Py_DECREF(u);
Py_XDECREF(v);
Py_XDECREF(w);
if (err == 0) continue;
break;
case STORE_SUBSCR:
w = POP();
v = POP();
u = POP();
/* v[w] = u */
err = PyObject_SetItem(v, w, u);
Py_DECREF(u);
Py_DECREF(v);
Py_DECREF(w);
if (err == 0) continue;
break;
case DELETE_SUBSCR:
w = POP();