1 
2 /* Execute compiled code */
3 
4 /* XXX TO DO:
5    XXX speed up searching for keywords by using a dictionary
6    XXX document it!
7    */
8 
9 /* enable more aggressive intra-module optimizations, where available */
10 #define PY_LOCAL_AGGRESSIVE
11 
12 #include "Python.h"
13 
14 #include "code.h"
15 #include "frameobject.h"
16 #include "eval.h"
17 #include "opcode.h"
18 #include "structmember.h"
19 
20 #include <ctype.h>
21 
22 #ifndef WITH_TSC
23 
24 #define READ_TIMESTAMP(var)
25 
26 #else
27 
28 typedef unsigned long long uint64;
29 
30 /* PowerPC support.
31    "__ppc__" appears to be the preprocessor definition to detect on OS X, whereas
32    "__powerpc__" appears to be the correct one for Linux with GCC
33 */
34 #if defined(__ppc__) || defined (__powerpc__)
35 
36 #define READ_TIMESTAMP(var) ppc_getcounter(&var)
37 
38 static void
ppc_getcounter(uint64 * v)39 ppc_getcounter(uint64 *v)
40 {
41     register unsigned long tbu, tb, tbu2;
42 
43   loop:
44     asm volatile ("mftbu %0" : "=r" (tbu) );
45     asm volatile ("mftb  %0" : "=r" (tb)  );
46     asm volatile ("mftbu %0" : "=r" (tbu2));
47     if (__builtin_expect(tbu != tbu2, 0)) goto loop;
48 
49     /* The slightly peculiar way of writing the next lines is
50        compiled better by GCC than any other way I tried. */
51     ((long*)(v))[0] = tbu;
52     ((long*)(v))[1] = tb;
53 }
54 
55 #elif defined(__i386__)
56 
57 /* this is for linux/x86 (and probably any other GCC/x86 combo) */
58 
59 #define READ_TIMESTAMP(val) \
60      __asm__ __volatile__("rdtsc" : "=A" (val))
61 
62 #elif defined(__x86_64__)
63 
64 /* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
65    not edx:eax as it does for i386.  Since rdtsc puts its result in edx:eax
66    even in 64-bit mode, we need to use "a" and "d" for the lower and upper
67    32-bit pieces of the result. */
68 
69 #define READ_TIMESTAMP(val) do {                        \
70     unsigned int h, l;                                  \
71     __asm__ __volatile__("rdtsc" : "=a" (l), "=d" (h)); \
72     (val) = ((uint64)l) | (((uint64)h) << 32);          \
73     } while(0)
74 
75 
76 #else
77 
78 #error "Don't know how to implement timestamp counter for this architecture"
79 
80 #endif
81 
dump_tsc(int opcode,int ticked,uint64 inst0,uint64 inst1,uint64 loop0,uint64 loop1,uint64 intr0,uint64 intr1)82 void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
83               uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
84 {
85     uint64 intr, inst, loop;
86     PyThreadState *tstate = PyThreadState_Get();
87     if (!tstate->interp->tscdump)
88         return;
89     intr = intr1 - intr0;
90     inst = inst1 - inst0 - intr;
91     loop = loop1 - loop0 - intr;
92     fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
93             opcode, ticked, inst, loop);
94 }
95 
96 #endif
97 
98 /* Turn this on if your compiler chokes on the big switch: */
99 /* #define CASE_TOO_BIG 1 */
100 
101 #ifdef Py_DEBUG
102 /* For debugging the interpreter: */
103 #define LLTRACE  1      /* Low-level trace feature */
104 #define CHECKEXC 1      /* Double-check exception checking */
105 #endif
106 
107 typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
108 
109 /* Forward declarations */
110 #ifdef WITH_TSC
111 static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
112 #else
113 static PyObject * call_function(PyObject ***, int);
114 #endif
115 static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
116 static PyObject * do_call(PyObject *, PyObject ***, int, int);
117 static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
118 static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
119                                       PyObject *);
120 static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
121 static PyObject * load_args(PyObject ***, int);
122 #define CALL_FLAG_VAR 1
123 #define CALL_FLAG_KW 2
124 
125 #ifdef LLTRACE
126 static int lltrace;
127 static int prtrace(PyObject *, char *);
128 #endif
129 static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
130                       int, PyObject *);
131 static int call_trace_protected(Py_tracefunc, PyObject *,
132                                 PyFrameObject *, int, PyObject *);
133 static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
134 static int maybe_call_line_trace(Py_tracefunc, PyObject *,
135                                  PyFrameObject *, int *, int *, int *);
136 
137 static PyObject * apply_slice(PyObject *, PyObject *, PyObject *);
138 static int assign_slice(PyObject *, PyObject *,
139                         PyObject *, PyObject *);
140 static PyObject * cmp_outcome(int, PyObject *, PyObject *);
141 static PyObject * import_from(PyObject *, PyObject *);
142 static int import_all_from(PyObject *, PyObject *);
143 static PyObject * build_class(PyObject *, PyObject *, PyObject *);
144 static int exec_statement(PyFrameObject *,
145                           PyObject *, PyObject *, PyObject *);
146 static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
147 static void reset_exc_info(PyThreadState *);
148 static void format_exc_check_arg(PyObject *, char *, PyObject *);
149 static PyObject * string_concatenate(PyObject *, PyObject *,
150                                      PyFrameObject *, unsigned char *);
151 static PyObject * kwd_as_string(PyObject *);
152 static PyObject * special_lookup(PyObject *, char *, PyObject **);
153 
154 #define NAME_ERROR_MSG \
155     "name '%.200s' is not defined"
156 #define GLOBAL_NAME_ERROR_MSG \
157     "global name '%.200s' is not defined"
158 #define UNBOUNDLOCAL_ERROR_MSG \
159     "local variable '%.200s' referenced before assignment"
160 #define UNBOUNDFREE_ERROR_MSG \
161     "free variable '%.200s' referenced before assignment" \
162     " in enclosing scope"
163 
164 /* Dynamic execution profile */
165 #ifdef DYNAMIC_EXECUTION_PROFILE
166 #ifdef DXPAIRS
167 static long dxpairs[257][256];
168 #define dxp dxpairs[256]
169 #else
170 static long dxp[256];
171 #endif
172 #endif
173 
174 /* Function call profile */
175 #ifdef CALL_PROFILE
176 #define PCALL_NUM 11
177 static int pcall[PCALL_NUM];
178 
179 #define PCALL_ALL 0
180 #define PCALL_FUNCTION 1
181 #define PCALL_FAST_FUNCTION 2
182 #define PCALL_FASTER_FUNCTION 3
183 #define PCALL_METHOD 4
184 #define PCALL_BOUND_METHOD 5
185 #define PCALL_CFUNCTION 6
186 #define PCALL_TYPE 7
187 #define PCALL_GENERATOR 8
188 #define PCALL_OTHER 9
189 #define PCALL_POP 10
190 
191 /* Notes about the statistics
192 
193    PCALL_FAST stats
194 
195    FAST_FUNCTION means no argument tuple needs to be created.
196    FASTER_FUNCTION means that the fast-path frame setup code is used.
197 
198    If there is a method call where the call can be optimized by changing
199    the argument tuple and calling the function directly, it gets recorded
200    twice.
201 
202    As a result, the relationship among the statistics appears to be
203    PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
204                 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
205    PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
206    PCALL_METHOD > PCALL_BOUND_METHOD
207 */
208 
209 #define PCALL(POS) pcall[POS]++
210 
211 PyObject *
PyEval_GetCallStats(PyObject * self)212 PyEval_GetCallStats(PyObject *self)
213 {
214     return Py_BuildValue("iiiiiiiiiii",
215                          pcall[0], pcall[1], pcall[2], pcall[3],
216                          pcall[4], pcall[5], pcall[6], pcall[7],
217                          pcall[8], pcall[9], pcall[10]);
218 }
219 #else
220 #define PCALL(O)
221 
222 PyObject *
PyEval_GetCallStats(PyObject * self)223 PyEval_GetCallStats(PyObject *self)
224 {
225     Py_INCREF(Py_None);
226     return Py_None;
227 }
228 #endif
229 
230 
231 #ifdef WITH_THREAD
232 
233 #ifdef HAVE_ERRNO_H
234 #include <errno.h>
235 #endif
236 #include "pythread.h"
237 
238 static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
239 static PyThread_type_lock pending_lock = 0; /* for pending calls */
240 static long main_thread = 0;
241 
242 int
PyEval_ThreadsInitialized(void)243 PyEval_ThreadsInitialized(void)
244 {
245     return interpreter_lock != 0;
246 }
247 
248 void
PyEval_InitThreads(void)249 PyEval_InitThreads(void)
250 {
251     if (interpreter_lock)
252         return;
253     interpreter_lock = PyThread_allocate_lock();
254     PyThread_acquire_lock(interpreter_lock, 1);
255     main_thread = PyThread_get_thread_ident();
256 }
257 
258 void
PyEval_AcquireLock(void)259 PyEval_AcquireLock(void)
260 {
261     PyThread_acquire_lock(interpreter_lock, 1);
262 }
263 
264 void
PyEval_ReleaseLock(void)265 PyEval_ReleaseLock(void)
266 {
267     PyThread_release_lock(interpreter_lock);
268 }
269 
270 void
PyEval_AcquireThread(PyThreadState * tstate)271 PyEval_AcquireThread(PyThreadState *tstate)
272 {
273     if (tstate == NULL)
274         Py_FatalError("PyEval_AcquireThread: NULL new thread state");
275     /* Check someone has called PyEval_InitThreads() to create the lock */
276     assert(interpreter_lock);
277     PyThread_acquire_lock(interpreter_lock, 1);
278     if (PyThreadState_Swap(tstate) != NULL)
279         Py_FatalError(
280             "PyEval_AcquireThread: non-NULL old thread state");
281 }
282 
283 void
PyEval_ReleaseThread(PyThreadState * tstate)284 PyEval_ReleaseThread(PyThreadState *tstate)
285 {
286     if (tstate == NULL)
287         Py_FatalError("PyEval_ReleaseThread: NULL thread state");
288     if (PyThreadState_Swap(NULL) != tstate)
289         Py_FatalError("PyEval_ReleaseThread: wrong thread state");
290     PyThread_release_lock(interpreter_lock);
291 }
292 
293 /* This function is called from PyOS_AfterFork to ensure that newly
294    created child processes don't hold locks referring to threads which
295    are not running in the child process.  (This could also be done using
296    pthread_atfork mechanism, at least for the pthreads implementation.) */
297 
298 void
PyEval_ReInitThreads(void)299 PyEval_ReInitThreads(void)
300 {
301     PyObject *threading, *result;
302     PyThreadState *tstate;
303 
304     if (!interpreter_lock)
305         return;
306     /*XXX Can't use PyThread_free_lock here because it does too
307       much error-checking.  Doing this cleanly would require
308       adding a new function to each thread_*.h.  Instead, just
309       create a new lock and waste a little bit of memory */
310     interpreter_lock = PyThread_allocate_lock();
311     pending_lock = PyThread_allocate_lock();
312     PyThread_acquire_lock(interpreter_lock, 1);
313     main_thread = PyThread_get_thread_ident();
314 
315     /* Update the threading module with the new state.
316      */
317     tstate = PyThreadState_GET();
318     threading = PyMapping_GetItemString(tstate->interp->modules,
319                                         "threading");
320     if (threading == NULL) {
321         /* threading not imported */
322         PyErr_Clear();
323         return;
324     }
325     result = PyObject_CallMethod(threading, "_after_fork", NULL);
326     if (result == NULL)
327         PyErr_WriteUnraisable(threading);
328     else
329         Py_DECREF(result);
330     Py_DECREF(threading);
331 }
332 #endif
333 
334 /* Functions save_thread and restore_thread are always defined so
335    dynamically loaded modules needn't be compiled separately for use
336    with and without threads: */
337 
338 PyThreadState *
PyEval_SaveThread(void)339 PyEval_SaveThread(void)
340 {
341     PyThreadState *tstate = PyThreadState_Swap(NULL);
342     if (tstate == NULL)
343         Py_FatalError("PyEval_SaveThread: NULL tstate");
344 #ifdef WITH_THREAD
345     if (interpreter_lock)
346         PyThread_release_lock(interpreter_lock);
347 #endif
348     return tstate;
349 }
350 
351 void
PyEval_RestoreThread(PyThreadState * tstate)352 PyEval_RestoreThread(PyThreadState *tstate)
353 {
354     if (tstate == NULL)
355         Py_FatalError("PyEval_RestoreThread: NULL tstate");
356 #ifdef WITH_THREAD
357     if (interpreter_lock) {
358         int err = errno;
359         PyThread_acquire_lock(interpreter_lock, 1);
360         errno = err;
361     }
362 #endif
363     PyThreadState_Swap(tstate);
364 }
365 
366 
367 /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
368    signal handlers or Mac I/O completion routines) can schedule calls
369    to a function to be called synchronously.
370    The synchronous function is called with one void* argument.
371    It should return 0 for success or -1 for failure -- failure should
372    be accompanied by an exception.
373 
374    If registry succeeds, the registry function returns 0; if it fails
375    (e.g. due to too many pending calls) it returns -1 (without setting
376    an exception condition).
377 
378    Note that because registry may occur from within signal handlers,
379    or other asynchronous events, calling malloc() is unsafe!
380 
381 #ifdef WITH_THREAD
382    Any thread can schedule pending calls, but only the main thread
383    will execute them.
384    There is no facility to schedule calls to a particular thread, but
385    that should be easy to change, should that ever be required.  In
386    that case, the static variables here should go into the python
387    threadstate.
388 #endif
389 */
390 
391 #ifdef WITH_THREAD
392 
393 /* The WITH_THREAD implementation is thread-safe.  It allows
394    scheduling to be made from any thread, and even from an executing
395    callback.
396  */
397 
398 #define NPENDINGCALLS 32
399 static struct {
400     int (*func)(void *);
401     void *arg;
402 } pendingcalls[NPENDINGCALLS];
403 static int pendingfirst = 0;
404 static int pendinglast = 0;
405 static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */
406 static char pendingbusy = 0;
407 
408 int
Py_AddPendingCall(int (* func)(void *),void * arg)409 Py_AddPendingCall(int (*func)(void *), void *arg)
410 {
411     int i, j, result=0;
412     PyThread_type_lock lock = pending_lock;
413 
414     /* try a few times for the lock.  Since this mechanism is used
415      * for signal handling (on the main thread), there is a (slim)
416      * chance that a signal is delivered on the same thread while we
417      * hold the lock during the Py_MakePendingCalls() function.
418      * This avoids a deadlock in that case.
419      * Note that signals can be delivered on any thread.  In particular,
420      * on Windows, a SIGINT is delivered on a system-created worker
421      * thread.
422      * We also check for lock being NULL, in the unlikely case that
423      * this function is called before any bytecode evaluation takes place.
424      */
425     if (lock != NULL) {
426         for (i = 0; i<100; i++) {
427             if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
428                 break;
429         }
430         if (i == 100)
431             return -1;
432     }
433 
434     i = pendinglast;
435     j = (i + 1) % NPENDINGCALLS;
436     if (j == pendingfirst) {
437         result = -1; /* Queue full */
438     } else {
439         pendingcalls[i].func = func;
440         pendingcalls[i].arg = arg;
441         pendinglast = j;
442     }
443     /* signal main loop */
444     _Py_Ticker = 0;
445     pendingcalls_to_do = 1;
446     if (lock != NULL)
447         PyThread_release_lock(lock);
448     return result;
449 }
450 
451 int
Py_MakePendingCalls(void)452 Py_MakePendingCalls(void)
453 {
454     int i;
455     int r = 0;
456 
457     if (!pending_lock) {
458         /* initial allocation of the lock */
459         pending_lock = PyThread_allocate_lock();
460         if (pending_lock == NULL)
461             return -1;
462     }
463 
464     /* only service pending calls on main thread */
465     if (main_thread && PyThread_get_thread_ident() != main_thread)
466         return 0;
467     /* don't perform recursive pending calls */
468     if (pendingbusy)
469         return 0;
470     pendingbusy = 1;
471     /* perform a bounded number of calls, in case of recursion */
472     for (i=0; i<NPENDINGCALLS; i++) {
473         int j;
474         int (*func)(void *);
475         void *arg = NULL;
476 
477         /* pop one item off the queue while holding the lock */
478         PyThread_acquire_lock(pending_lock, WAIT_LOCK);
479         j = pendingfirst;
480         if (j == pendinglast) {
481             func = NULL; /* Queue empty */
482         } else {
483             func = pendingcalls[j].func;
484             arg = pendingcalls[j].arg;
485             pendingfirst = (j + 1) % NPENDINGCALLS;
486         }
487         pendingcalls_to_do = pendingfirst != pendinglast;
488         PyThread_release_lock(pending_lock);
489         /* having released the lock, perform the callback */
490         if (func == NULL)
491             break;
492         r = func(arg);
493         if (r)
494             break;
495     }
496     pendingbusy = 0;
497     return r;
498 }
499 
500 #else /* if ! defined WITH_THREAD */
501 
502 /*
503    WARNING!  ASYNCHRONOUSLY EXECUTING CODE!
504    This code is used for signal handling in python that isn't built
505    with WITH_THREAD.
506    Don't use this implementation when Py_AddPendingCalls() can happen
507    on a different thread!
508 
509    There are two possible race conditions:
510    (1) nested asynchronous calls to Py_AddPendingCall()
511    (2) AddPendingCall() calls made while pending calls are being processed.
512 
513    (1) is very unlikely because typically signal delivery
514    is blocked during signal handling.  So it should be impossible.
515    (2) is a real possibility.
516    The current code is safe against (2), but not against (1).
517    The safety against (2) is derived from the fact that only one
518    thread is present, interrupted by signals, and that the critical
519    section is protected with the "busy" variable.  On Windows, which
520    delivers SIGINT on a system thread, this does not hold and therefore
521    Windows really shouldn't use this version.
522    The two threads could theoretically wiggle around the "busy" variable.
523 */
524 
525 #define NPENDINGCALLS 32
526 static struct {
527     int (*func)(void *);
528     void *arg;
529 } pendingcalls[NPENDINGCALLS];
530 static volatile int pendingfirst = 0;
531 static volatile int pendinglast = 0;
532 static volatile int pendingcalls_to_do = 0;
533 
534 int
Py_AddPendingCall(int (* func)(void *),void * arg)535 Py_AddPendingCall(int (*func)(void *), void *arg)
536 {
537     static volatile int busy = 0;
538     int i, j;
539     /* XXX Begin critical section */
540     if (busy)
541         return -1;
542     busy = 1;
543     i = pendinglast;
544     j = (i + 1) % NPENDINGCALLS;
545     if (j == pendingfirst) {
546         busy = 0;
547         return -1; /* Queue full */
548     }
549     pendingcalls[i].func = func;
550     pendingcalls[i].arg = arg;
551     pendinglast = j;
552 
553     _Py_Ticker = 0;
554     pendingcalls_to_do = 1; /* Signal main loop */
555     busy = 0;
556     /* XXX End critical section */
557     return 0;
558 }
559 
560 int
Py_MakePendingCalls(void)561 Py_MakePendingCalls(void)
562 {
563     static int busy = 0;
564     if (busy)
565         return 0;
566     busy = 1;
567     pendingcalls_to_do = 0;
568     for (;;) {
569         int i;
570         int (*func)(void *);
571         void *arg;
572         i = pendingfirst;
573         if (i == pendinglast)
574             break; /* Queue empty */
575         func = pendingcalls[i].func;
576         arg = pendingcalls[i].arg;
577         pendingfirst = (i + 1) % NPENDINGCALLS;
578         if (func(arg) < 0) {
579             busy = 0;
580             pendingcalls_to_do = 1; /* We're not done yet */
581             return -1;
582         }
583     }
584     busy = 0;
585     return 0;
586 }
587 
588 #endif /* WITH_THREAD */
589 
590 
591 /* The interpreter's recursion limit */
592 
593 #ifndef Py_DEFAULT_RECURSION_LIMIT
594 #define Py_DEFAULT_RECURSION_LIMIT 1000
595 #endif
596 static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
597 int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
598 
599 int
Py_GetRecursionLimit(void)600 Py_GetRecursionLimit(void)
601 {
602     return recursion_limit;
603 }
604 
605 void
Py_SetRecursionLimit(int new_limit)606 Py_SetRecursionLimit(int new_limit)
607 {
608     recursion_limit = new_limit;
609     _Py_CheckRecursionLimit = recursion_limit;
610 }
611 
612 /* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
613    if the recursion_depth reaches _Py_CheckRecursionLimit.
614    If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
615    to guarantee that _Py_CheckRecursiveCall() is regularly called.
616    Without USE_STACKCHECK, there is no need for this. */
617 int
_Py_CheckRecursiveCall(const char * where)618 _Py_CheckRecursiveCall(const char *where)
619 {
620     PyThreadState *tstate = PyThreadState_GET();
621 
622 #ifdef USE_STACKCHECK
623     if (PyOS_CheckStack()) {
624         --tstate->recursion_depth;
625         PyErr_SetString(PyExc_MemoryError, "Stack overflow");
626         return -1;
627     }
628 #endif
629     if (tstate->recursion_depth > recursion_limit) {
630         --tstate->recursion_depth;
631         PyErr_Format(PyExc_RuntimeError,
632                      "maximum recursion depth exceeded%s",
633                      where);
634         return -1;
635     }
636     _Py_CheckRecursionLimit = recursion_limit;
637     return 0;
638 }
639 
640 /* Status code for main loop (reason for stack unwind) */
641 enum why_code {
642         WHY_NOT =       0x0001, /* No error */
643         WHY_EXCEPTION = 0x0002, /* Exception occurred */
644         WHY_RERAISE =   0x0004, /* Exception re-raised by 'finally' */
645         WHY_RETURN =    0x0008, /* 'return' statement */
646         WHY_BREAK =     0x0010, /* 'break' statement */
647         WHY_CONTINUE =  0x0020, /* 'continue' statement */
648         WHY_YIELD =     0x0040  /* 'yield' operator */
649 };
650 
651 static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
652 static int unpack_iterable(PyObject *, int, PyObject **);
653 
654 /* Records whether tracing is on for any thread.  Counts the number of
655    threads for which tstate->c_tracefunc is non-NULL, so if the value
656    is 0, we know we don't have to check this thread's c_tracefunc.
657    This speeds up the if statement in PyEval_EvalFrameEx() after
658    fast_next_opcode*/
659 static int _Py_TracingPossible = 0;
660 
661 /* for manipulating the thread switch and periodic "stuff" - used to be
662    per thread, now just a pair o' globals */
663 int _Py_CheckInterval = 100;
664 volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */
665 
666 PyObject *
PyEval_EvalCode(PyCodeObject * co,PyObject * globals,PyObject * locals)667 PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
668 {
669     return PyEval_EvalCodeEx(co,
670                       globals, locals,
671                       (PyObject **)NULL, 0,
672                       (PyObject **)NULL, 0,
673                       (PyObject **)NULL, 0,
674                       NULL);
675 }
676 
677 
678 /* Interpreter main loop */
679 
680 PyObject *
PyEval_EvalFrame(PyFrameObject * f)681 PyEval_EvalFrame(PyFrameObject *f) {
682     /* This is for backward compatibility with extension modules that
683        used this API; core interpreter code should call
684        PyEval_EvalFrameEx() */
685     return PyEval_EvalFrameEx(f, 0);
686 }
687 
688 PyObject *
PyEval_EvalFrameEx(PyFrameObject * f,int throwflag)689 PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
690 {
691 #ifdef DYNAMIC_EXECUTION_PROFILE
692     #undef USE_COMPUTED_GOTOS
693 #endif
694 #ifdef HAVE_COMPUTED_GOTOS
695     #ifndef USE_COMPUTED_GOTOS
696         #if defined(__clang__) && (__clang_major__ < 5)
697             /* Computed gotos caused significant performance regression
698              * with clang < 5.0.
699              * https://bugs.python.org/issue32616
700              */
701             #define USE_COMPUTED_GOTOS 0
702         #else
703             #define USE_COMPUTED_GOTOS 1
704         #endif
705     #endif
706 #else
707     #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
708     #error "Computed gotos are not supported on this compiler."
709     #endif
710     #undef USE_COMPUTED_GOTOS
711     #define USE_COMPUTED_GOTOS 0
712 #endif
713 #if USE_COMPUTED_GOTOS
714 /* Import the static jump table */
715 #include "opcode_targets.h"
716 
717   /* This macro is used when several opcodes defer to the same implementation
718    (e.g. SETUP_LOOP, SETUP_FINALLY) */
719 #define TARGET_WITH_IMPL(op, impl) \
720         TARGET_##op: \
721         opcode = op; \
722         oparg = NEXTARG(); \
723         case op: \
724         goto impl; \
725 
726 #define TARGET_WITH_IMPL_NOARG(op, impl) \
727         TARGET_##op: \
728         opcode = op; \
729         case op: \
730         goto impl; \
731 
732 #define TARGET_NOARG(op) \
733         TARGET_##op: \
734         opcode = op; \
735         case op:\
736 
737 #define TARGET(op) \
738         TARGET_##op: \
739         opcode = op; \
740         oparg = NEXTARG(); \
741         case op:\
742 
743 
744 #define DISPATCH() \
745         { \
746     int _tick = _Py_Ticker - 1; \
747     _Py_Ticker = _tick; \
748     if (_tick >= 0) { \
749         FAST_DISPATCH(); \
750     } \
751     continue; \
752         }
753 
754 #ifdef LLTRACE
755 #define FAST_DISPATCH() \
756         { \
757     if (!lltrace && !_Py_TracingPossible) { \
758         f->f_lasti = INSTR_OFFSET(); \
759         goto *opcode_targets[*next_instr++]; \
760     } \
761     goto fast_next_opcode; \
762         }
763 #else
764 #define FAST_DISPATCH() { \
765         if (!_Py_TracingPossible) { \
766             f->f_lasti = INSTR_OFFSET(); \
767             goto *opcode_targets[*next_instr++]; \
768         } \
769         goto fast_next_opcode;\
770 }
771 #endif
772 
773 #else
774 #define TARGET(op) \
775         case op:
776 #define TARGET_WITH_IMPL(op, impl) \
777         /* silence compiler warnings about `impl` unused */ \
778         if (0) goto impl; \
779         case op:\
780 
781 #define TARGET_NOARG(op) \
782         case op:\
783 
784 #define TARGET_WITH_IMPL_NOARG(op, impl) \
785         if (0) goto impl; \
786         case op:\
787 
788 #define DISPATCH() continue
789 #define FAST_DISPATCH() goto fast_next_opcode
790 #endif
791 
792 
793 #ifdef DXPAIRS
794     int lastopcode = 0;
795 #endif
796     register PyObject **stack_pointer;  /* Next free slot in value stack */
797     register unsigned char *next_instr;
798     register int opcode;        /* Current opcode */
799     register int oparg;         /* Current opcode argument, if any */
800     register enum why_code why; /* Reason for block stack unwind */
801     register int err;           /* Error status -- nonzero if error */
802     register PyObject *x;       /* Result object -- NULL if error */
803     register PyObject *v;       /* Temporary objects popped off stack */
804     register PyObject *w;
805     register PyObject *u;
806     register PyObject *t;
807     register PyObject *stream = NULL;    /* for PRINT opcodes */
808     register PyObject **fastlocals, **freevars;
809     PyObject *retval = NULL;            /* Return value */
810     PyThreadState *tstate = PyThreadState_GET();
811     PyCodeObject *co;
812 
813     /* when tracing we set things up so that
814 
815            not (instr_lb <= current_bytecode_offset < instr_ub)
816 
817        is true when the line being executed has changed.  The
818        initial values are such as to make this false the first
819        time it is tested. */
820     int instr_ub = -1, instr_lb = 0, instr_prev = -1;
821 
822     unsigned char *first_instr;
823     PyObject *names;
824     PyObject *consts;
825 #if defined(Py_DEBUG) || defined(LLTRACE)
826     /* Make it easier to find out where we are with a debugger */
827 #ifdef __GNUC__
828     char *filename __attribute__((unused));
829 #else
830     char *filename;
831 #endif
832 #endif
833 
834 /* Tuple access macros */
835 
836 #ifndef Py_DEBUG
837 #define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
838 #else
839 #define GETITEM(v, i) PyTuple_GetItem((v), (i))
840 #endif
841 
842 #ifdef WITH_TSC
843 /* Use Pentium timestamp counter to mark certain events:
844    inst0 -- beginning of switch statement for opcode dispatch
845    inst1 -- end of switch statement (may be skipped)
846    loop0 -- the top of the mainloop
847    loop1 -- place where control returns again to top of mainloop
848             (may be skipped)
849    intr1 -- beginning of long interruption
850    intr2 -- end of long interruption
851 
852    Many opcodes call out to helper C functions.  In some cases, the
853    time in those functions should be counted towards the time for the
854    opcode, but not in all cases.  For example, a CALL_FUNCTION opcode
855    calls another Python function; there's no point in charge all the
856    bytecode executed by the called function to the caller.
857 
858    It's hard to make a useful judgement statically.  In the presence
859    of operator overloading, it's impossible to tell if a call will
860    execute new Python code or not.
861 
862    It's a case-by-case judgement.  I'll use intr1 for the following
863    cases:
864 
865    EXEC_STMT
866    IMPORT_STAR
867    IMPORT_FROM
868    CALL_FUNCTION (and friends)
869 
870  */
871     uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
872     int ticked = 0;
873 
874     READ_TIMESTAMP(inst0);
875     READ_TIMESTAMP(inst1);
876     READ_TIMESTAMP(loop0);
877     READ_TIMESTAMP(loop1);
878 
879     /* shut up the compiler */
880     opcode = 0;
881 #endif
882 
883 /* Code access macros */
884 
885 #define INSTR_OFFSET()  ((int)(next_instr - first_instr))
886 #define NEXTOP()        (*next_instr++)
887 #define NEXTARG()       (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
888 #define PEEKARG()       ((next_instr[2]<<8) + next_instr[1])
889 #define JUMPTO(x)       (next_instr = first_instr + (x))
890 #define JUMPBY(x)       (next_instr += (x))
891 
892 /* OpCode prediction macros
893     Some opcodes tend to come in pairs thus making it possible to
894     predict the second code when the first is run.  For example,
895     GET_ITER is often followed by FOR_ITER. And FOR_ITER is often
896     followed by STORE_FAST or UNPACK_SEQUENCE.
897 
898     Verifying the prediction costs a single high-speed test of a register
899     variable against a constant.  If the pairing was good, then the
900     processor's own internal branch predication has a high likelihood of
901     success, resulting in a nearly zero-overhead transition to the
902     next opcode.  A successful prediction saves a trip through the eval-loop
903     including its two unpredictable branches, the HAS_ARG test and the
904     switch-case.  Combined with the processor's internal branch prediction,
905     a successful PREDICT has the effect of making the two opcodes run as if
906     they were a single new opcode with the bodies combined.
907 
908     If collecting opcode statistics, your choices are to either keep the
909     predictions turned-on and interpret the results as if some opcodes
910     had been combined or turn-off predictions so that the opcode frequency
911     counter updates for both opcodes.
912 */
913 
914 
915 #if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
916 #define PREDICT(op)             if (0) goto PRED_##op
917 #define PREDICTED(op)           PRED_##op:
918 #define PREDICTED_WITH_ARG(op)  PRED_##op:
919 #else
920 #define PREDICT(op)             if (*next_instr == op) goto PRED_##op
921 #define PREDICTED(op)           PRED_##op: next_instr++
922 #define PREDICTED_WITH_ARG(op)  PRED_##op: oparg = PEEKARG(); next_instr += 3
923 #endif
924 
925 
926 /* Stack manipulation macros */
927 
928 /* The stack can grow at most MAXINT deep, as co_nlocals and
929    co_stacksize are ints. */
930 #define STACK_LEVEL()     ((int)(stack_pointer - f->f_valuestack))
931 #define EMPTY()           (STACK_LEVEL() == 0)
932 #define TOP()             (stack_pointer[-1])
933 #define SECOND()          (stack_pointer[-2])
934 #define THIRD()           (stack_pointer[-3])
935 #define FOURTH()          (stack_pointer[-4])
936 #define PEEK(n)           (stack_pointer[-(n)])
937 #define SET_TOP(v)        (stack_pointer[-1] = (v))
938 #define SET_SECOND(v)     (stack_pointer[-2] = (v))
939 #define SET_THIRD(v)      (stack_pointer[-3] = (v))
940 #define SET_FOURTH(v)     (stack_pointer[-4] = (v))
941 #define SET_VALUE(n, v)   (stack_pointer[-(n)] = (v))
942 #define BASIC_STACKADJ(n) (stack_pointer += n)
943 #define BASIC_PUSH(v)     (*stack_pointer++ = (v))
944 #define BASIC_POP()       (*--stack_pointer)
945 
946 #ifdef LLTRACE
947 #define PUSH(v)         { (void)(BASIC_PUSH(v), \
948                           lltrace && prtrace(TOP(), "push")); \
949                           assert(STACK_LEVEL() <= co->co_stacksize); }
950 #define POP()           ((void)(lltrace && prtrace(TOP(), "pop")), \
951                          BASIC_POP())
952 #define STACKADJ(n)     { (void)(BASIC_STACKADJ(n), \
953                           lltrace && prtrace(TOP(), "stackadj")); \
954                           assert(STACK_LEVEL() <= co->co_stacksize); }
955 #define EXT_POP(STACK_POINTER) ((void)(lltrace && \
956                                 prtrace((STACK_POINTER)[-1], "ext_pop")), \
957                                 *--(STACK_POINTER))
958 #else
959 #define PUSH(v)                BASIC_PUSH(v)
960 #define POP()                  BASIC_POP()
961 #define STACKADJ(n)            BASIC_STACKADJ(n)
962 #define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
963 #endif
964 
965 /* Local variable macros */
966 
967 #define GETLOCAL(i)     (fastlocals[i])
968 
969 /* The SETLOCAL() macro must not DECREF the local variable in-place and
970    then store the new value; it must copy the old value to a temporary
971    value, then store the new value, and then DECREF the temporary value.
972    This is because it is possible that during the DECREF the frame is
973    accessed by other code (e.g. a __del__ method or gc.collect()) and the
974    variable would be pointing to already-freed memory. */
975 #define SETLOCAL(i, value)      do { PyObject *tmp = GETLOCAL(i); \
976                                      GETLOCAL(i) = value; \
977                                      Py_XDECREF(tmp); } while (0)
978 
979 /* Start of code */
980 
981     if (f == NULL)
982         return NULL;
983 
984     /* push frame */
985     if (Py_EnterRecursiveCall(""))
986         return NULL;
987 
988     tstate->frame = f;
989 
990     if (tstate->use_tracing) {
991         if (tstate->c_tracefunc != NULL) {
992             /* tstate->c_tracefunc, if defined, is a
993                function that will be called on *every* entry
994                to a code block.  Its return value, if not
995                None, is a function that will be called at
996                the start of each executed line of code.
997                (Actually, the function must return itself
998                in order to continue tracing.)  The trace
999                functions are called with three arguments:
1000                a pointer to the current frame, a string
1001                indicating why the function is called, and
1002                an argument which depends on the situation.
1003                The global trace function is also called
1004                whenever an exception is detected. */
1005             if (call_trace_protected(tstate->c_tracefunc,
1006                                      tstate->c_traceobj,
1007                                      f, PyTrace_CALL, Py_None)) {
1008                 /* Trace function raised an error */
1009                 goto exit_eval_frame;
1010             }
1011         }
1012         if (tstate->c_profilefunc != NULL) {
1013             /* Similar for c_profilefunc, except it needn't
1014                return itself and isn't called for "line" events */
1015             if (call_trace_protected(tstate->c_profilefunc,
1016                                      tstate->c_profileobj,
1017                                      f, PyTrace_CALL, Py_None)) {
1018                 /* Profile function raised an error */
1019                 goto exit_eval_frame;
1020             }
1021         }
1022     }
1023 
1024     co = f->f_code;
1025     names = co->co_names;
1026     consts = co->co_consts;
1027     fastlocals = f->f_localsplus;
1028     freevars = f->f_localsplus + co->co_nlocals;
1029     first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
1030     /* An explanation is in order for the next line.
1031 
1032        f->f_lasti now refers to the index of the last instruction
1033        executed.  You might think this was obvious from the name, but
1034        this wasn't always true before 2.3!  PyFrame_New now sets
1035        f->f_lasti to -1 (i.e. the index *before* the first instruction)
1036        and YIELD_VALUE doesn't fiddle with f_lasti any more.  So this
1037        does work.  Promise.
1038 
1039        When the PREDICT() macros are enabled, some opcode pairs follow in
1040        direct succession without updating f->f_lasti.  A successful
1041        prediction effectively links the two codes together as if they
1042        were a single new opcode; accordingly,f->f_lasti will point to
1043        the first code in the pair (for instance, GET_ITER followed by
1044        FOR_ITER is effectively a single opcode and f->f_lasti will point
1045        at to the beginning of the combined pair.)
1046     */
1047     next_instr = first_instr + f->f_lasti + 1;
1048     stack_pointer = f->f_stacktop;
1049     assert(stack_pointer != NULL);
1050     f->f_stacktop = NULL;       /* remains NULL unless yield suspends frame */
1051 
1052 #ifdef LLTRACE
1053     lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
1054 #endif
1055 #if defined(Py_DEBUG) || defined(LLTRACE)
1056     filename = PyString_AsString(co->co_filename);
1057 #endif
1058 
1059     why = WHY_NOT;
1060     err = 0;
1061     x = Py_None;        /* Not a reference, just anything non-NULL */
1062     w = NULL;
1063 
1064     if (throwflag) { /* support for generator.throw() */
1065         why = WHY_EXCEPTION;
1066         goto on_error;
1067     }
1068 
1069     for (;;) {
1070 #ifdef WITH_TSC
1071         if (inst1 == 0) {
1072             /* Almost surely, the opcode executed a break
1073                or a continue, preventing inst1 from being set
1074                on the way out of the loop.
1075             */
1076             READ_TIMESTAMP(inst1);
1077             loop1 = inst1;
1078         }
1079         dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1080                  intr0, intr1);
1081         ticked = 0;
1082         inst1 = 0;
1083         intr0 = 0;
1084         intr1 = 0;
1085         READ_TIMESTAMP(loop0);
1086 #endif
1087         assert(stack_pointer >= f->f_valuestack); /* else underflow */
1088         assert(STACK_LEVEL() <= co->co_stacksize);  /* else overflow */
1089 
1090         /* Do periodic things.  Doing this every time through
1091            the loop would add too much overhead, so we do it
1092            only every Nth instruction.  We also do it if
1093            ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1094            event needs attention (e.g. a signal handler or
1095            async I/O handler); see Py_AddPendingCall() and
1096            Py_MakePendingCalls() above. */
1097 
1098         if (--_Py_Ticker < 0) {
1099             if (*next_instr == SETUP_FINALLY) {
1100                 /* Make the last opcode before
1101                    a try: finally: block uninterruptible. */
1102                 goto fast_next_opcode;
1103             }
1104             _Py_Ticker = _Py_CheckInterval;
1105             tstate->tick_counter++;
1106 #ifdef WITH_TSC
1107             ticked = 1;
1108 #endif
1109             if (pendingcalls_to_do) {
1110                 if (Py_MakePendingCalls() < 0) {
1111                     why = WHY_EXCEPTION;
1112                     goto on_error;
1113                 }
1114                 if (pendingcalls_to_do)
1115                     /* MakePendingCalls() didn't succeed.
1116                        Force early re-execution of this
1117                        "periodic" code, possibly after
1118                        a thread switch */
1119                     _Py_Ticker = 0;
1120             }
1121 #ifdef WITH_THREAD
1122             if (interpreter_lock) {
1123                 /* Give another thread a chance */
1124 
1125                 if (PyThreadState_Swap(NULL) != tstate)
1126                     Py_FatalError("ceval: tstate mix-up");
1127                 PyThread_release_lock(interpreter_lock);
1128 
1129                 /* Other threads may run now */
1130 
1131                 PyThread_acquire_lock(interpreter_lock, 1);
1132 
1133                 if (PyThreadState_Swap(tstate) != NULL)
1134                     Py_FatalError("ceval: orphan tstate");
1135 
1136                 /* Check for thread interrupts */
1137 
1138                 if (tstate->async_exc != NULL) {
1139                     x = tstate->async_exc;
1140                     tstate->async_exc = NULL;
1141                     PyErr_SetNone(x);
1142                     Py_DECREF(x);
1143                     why = WHY_EXCEPTION;
1144                     goto on_error;
1145                 }
1146             }
1147 #endif
1148         }
1149 
1150     fast_next_opcode:
1151         f->f_lasti = INSTR_OFFSET();
1152 
1153         /* line-by-line tracing support */
1154 
1155         if (_Py_TracingPossible &&
1156             tstate->c_tracefunc != NULL && !tstate->tracing) {
1157             /* see maybe_call_line_trace
1158                for expository comments */
1159             f->f_stacktop = stack_pointer;
1160 
1161             err = maybe_call_line_trace(tstate->c_tracefunc,
1162                                         tstate->c_traceobj,
1163                                         f, &instr_lb, &instr_ub,
1164                                         &instr_prev);
1165             /* Reload possibly changed frame fields */
1166             JUMPTO(f->f_lasti);
1167             if (f->f_stacktop != NULL) {
1168                 stack_pointer = f->f_stacktop;
1169                 f->f_stacktop = NULL;
1170             }
1171             if (err) {
1172                 /* trace function raised an exception */
1173                 goto on_error;
1174             }
1175         }
1176 
1177         /* Extract opcode and argument */
1178 
1179         opcode = NEXTOP();
1180         oparg = 0;   /* allows oparg to be stored in a register because
1181             it doesn't have to be remembered across a full loop */
1182         if (HAS_ARG(opcode))
1183             oparg = NEXTARG();
1184     dispatch_opcode:
1185 #ifdef DYNAMIC_EXECUTION_PROFILE
1186 #ifdef DXPAIRS
1187         dxpairs[lastopcode][opcode]++;
1188         lastopcode = opcode;
1189 #endif
1190         dxp[opcode]++;
1191 #endif
1192 
1193 #ifdef LLTRACE
1194         /* Instruction tracing */
1195 
1196         if (lltrace) {
1197             if (HAS_ARG(opcode)) {
1198                 printf("%d: %d, %d\n",
1199                        f->f_lasti, opcode, oparg);
1200             }
1201             else {
1202                 printf("%d: %d\n",
1203                        f->f_lasti, opcode);
1204             }
1205         }
1206 #endif
1207 
1208         /* Main switch on opcode */
1209         READ_TIMESTAMP(inst0);
1210 
1211         switch (opcode) {
1212 
1213         /* BEWARE!
1214            It is essential that any operation that fails sets either
1215            x to NULL, err to nonzero, or why to anything but WHY_NOT,
1216            and that no operation that succeeds does this! */
1217 
1218         /* case STOP_CODE: this is an error! */
1219 
1220         TARGET_NOARG(NOP)
1221         {
1222             FAST_DISPATCH();
1223         }
1224 
1225         TARGET(LOAD_FAST)
1226         {
1227             x = GETLOCAL(oparg);
1228             if (x != NULL) {
1229                 Py_INCREF(x);
1230                 PUSH(x);
1231                 FAST_DISPATCH();
1232             }
1233             format_exc_check_arg(PyExc_UnboundLocalError,
1234                 UNBOUNDLOCAL_ERROR_MSG,
1235                 PyTuple_GetItem(co->co_varnames, oparg));
1236             break;
1237         }
1238 
1239         TARGET(LOAD_CONST)
1240         {
1241             x = GETITEM(consts, oparg);
1242             Py_INCREF(x);
1243             PUSH(x);
1244             FAST_DISPATCH();
1245         }
1246 
1247         PREDICTED_WITH_ARG(STORE_FAST);
1248         TARGET(STORE_FAST)
1249         {
1250             v = POP();
1251             SETLOCAL(oparg, v);
1252             FAST_DISPATCH();
1253         }
1254 
1255         TARGET_NOARG(POP_TOP)
1256         {
1257             v = POP();
1258             Py_DECREF(v);
1259             FAST_DISPATCH();
1260         }
1261 
1262         TARGET_NOARG(ROT_TWO)
1263         {
1264             v = TOP();
1265             w = SECOND();
1266             SET_TOP(w);
1267             SET_SECOND(v);
1268             FAST_DISPATCH();
1269         }
1270 
1271         TARGET_NOARG(ROT_THREE)
1272         {
1273             v = TOP();
1274             w = SECOND();
1275             x = THIRD();
1276             SET_TOP(w);
1277             SET_SECOND(x);
1278             SET_THIRD(v);
1279             FAST_DISPATCH();
1280         }
1281 
1282         TARGET_NOARG(ROT_FOUR)
1283          {
1284             u = TOP();
1285             v = SECOND();
1286             w = THIRD();
1287             x = FOURTH();
1288             SET_TOP(v);
1289             SET_SECOND(w);
1290             SET_THIRD(x);
1291             SET_FOURTH(u);
1292             FAST_DISPATCH();
1293         }
1294 
1295 
1296         TARGET_NOARG(DUP_TOP)
1297         {
1298             v = TOP();
1299             Py_INCREF(v);
1300             PUSH(v);
1301             FAST_DISPATCH();
1302         }
1303 
1304 
1305         TARGET(DUP_TOPX)
1306         {
1307             if (oparg == 2) {
1308                 x = TOP();
1309                 Py_INCREF(x);
1310                 w = SECOND();
1311                 Py_INCREF(w);
1312                 STACKADJ(2);
1313                 SET_TOP(x);
1314                 SET_SECOND(w);
1315                 FAST_DISPATCH();
1316             } else if (oparg == 3) {
1317                 x = TOP();
1318                 Py_INCREF(x);
1319                 w = SECOND();
1320                 Py_INCREF(w);
1321                 v = THIRD();
1322                 Py_INCREF(v);
1323                 STACKADJ(3);
1324                 SET_TOP(x);
1325                 SET_SECOND(w);
1326                 SET_THIRD(v);
1327                 FAST_DISPATCH();
1328             }
1329             Py_FatalError("invalid argument to DUP_TOPX"
1330                           " (bytecode corruption?)");
1331             /* Never returns, so don't bother to set why. */
1332             break;
1333         }
1334 
1335         TARGET_NOARG(UNARY_POSITIVE)
1336         {
1337             v = TOP();
1338             x = PyNumber_Positive(v);
1339             Py_DECREF(v);
1340             SET_TOP(x);
1341             if (x != NULL) DISPATCH();
1342             break;
1343         }
1344 
1345         TARGET_NOARG( UNARY_NEGATIVE)
1346         {
1347             v = TOP();
1348             x = PyNumber_Negative(v);
1349             Py_DECREF(v);
1350             SET_TOP(x);
1351             if (x != NULL) DISPATCH();
1352             break;
1353         }
1354 
1355         TARGET_NOARG(UNARY_NOT)
1356         {
1357             v = TOP();
1358             err = PyObject_IsTrue(v);
1359             Py_DECREF(v);
1360             if (err == 0) {
1361                 Py_INCREF(Py_True);
1362                 SET_TOP(Py_True);
1363                 DISPATCH();
1364             }
1365             else if (err > 0) {
1366                 Py_INCREF(Py_False);
1367                 SET_TOP(Py_False);
1368                 err = 0;
1369                 DISPATCH();
1370             }
1371             STACKADJ(-1);
1372             break;
1373         }
1374 
1375         TARGET_NOARG(UNARY_CONVERT)
1376         {
1377             v = TOP();
1378             x = PyObject_Repr(v);
1379             Py_DECREF(v);
1380             SET_TOP(x);
1381             if (x != NULL) DISPATCH();
1382             break;
1383         }
1384 
1385         TARGET_NOARG(UNARY_INVERT)
1386         {
1387             v = TOP();
1388             x = PyNumber_Invert(v);
1389             Py_DECREF(v);
1390             SET_TOP(x);
1391             if (x != NULL) DISPATCH();
1392             break;
1393         }
1394 
1395         TARGET_NOARG(BINARY_POWER)
1396         {
1397             w = POP();
1398             v = TOP();
1399             x = PyNumber_Power(v, w, Py_None);
1400             Py_DECREF(v);
1401             Py_DECREF(w);
1402             SET_TOP(x);
1403             if (x != NULL) DISPATCH();
1404             break;
1405         }
1406 
1407         TARGET_NOARG(BINARY_MULTIPLY)
1408         {
1409             w = POP();
1410             v = TOP();
1411             x = PyNumber_Multiply(v, w);
1412             Py_DECREF(v);
1413             Py_DECREF(w);
1414             SET_TOP(x);
1415             if(x!=NULL) DISPATCH();
1416             break;
1417         }
1418 
1419         TARGET_NOARG(BINARY_DIVIDE)
1420         {
1421             if (!_Py_QnewFlag) {
1422                 w = POP();
1423                 v = TOP();
1424                 x = PyNumber_Divide(v, w);
1425                 Py_DECREF(v);
1426                 Py_DECREF(w);
1427                 SET_TOP(x);
1428                 if (x != NULL) DISPATCH();
1429                 break;
1430             }
1431         }
1432         /* -Qnew is in effect:  fall through to BINARY_TRUE_DIVIDE */
1433         TARGET_NOARG(BINARY_TRUE_DIVIDE)
1434         {
1435             w = POP();
1436             v = TOP();
1437             x = PyNumber_TrueDivide(v, w);
1438             Py_DECREF(v);
1439             Py_DECREF(w);
1440             SET_TOP(x);
1441             if (x != NULL) DISPATCH();
1442             break;
1443         }
1444 
1445         TARGET_NOARG(BINARY_FLOOR_DIVIDE)
1446         {
1447             w = POP();
1448             v = TOP();
1449             x = PyNumber_FloorDivide(v, w);
1450             Py_DECREF(v);
1451             Py_DECREF(w);
1452             SET_TOP(x);
1453             if (x != NULL) DISPATCH();
1454             break;
1455         }
1456 
1457         TARGET_NOARG(BINARY_MODULO)
1458         {
1459             w = POP();
1460             v = TOP();
1461             if (PyString_CheckExact(v)
1462                 && (!PyString_Check(w) || PyString_CheckExact(w))) {
1463                 /* fast path; string formatting, but not if the RHS is a str subclass
1464                    (see issue28598) */
1465                 x = PyString_Format(v, w);
1466             } else {
1467                 x = PyNumber_Remainder(v, w);
1468             }
1469             Py_DECREF(v);
1470             Py_DECREF(w);
1471             SET_TOP(x);
1472             if (x != NULL) DISPATCH();
1473             break;
1474         }
1475 
1476         TARGET_NOARG(BINARY_ADD)
1477         {
1478             w = POP();
1479             v = TOP();
1480             if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1481                 /* INLINE: int + int */
1482                 register long a, b, i;
1483                 a = PyInt_AS_LONG(v);
1484                 b = PyInt_AS_LONG(w);
1485                 /* cast to avoid undefined behaviour
1486                    on overflow */
1487                 i = (long)((unsigned long)a + b);
1488                 if ((i^a) < 0 && (i^b) < 0)
1489                     goto slow_add;
1490                 x = PyInt_FromLong(i);
1491             }
1492             else if (PyString_CheckExact(v) &&
1493                      PyString_CheckExact(w)) {
1494                 x = string_concatenate(v, w, f, next_instr);
1495                 /* string_concatenate consumed the ref to v */
1496                 goto skip_decref_vx;
1497             }
1498             else {
1499               slow_add:
1500                 x = PyNumber_Add(v, w);
1501             }
1502             Py_DECREF(v);
1503           skip_decref_vx:
1504             Py_DECREF(w);
1505             SET_TOP(x);
1506             if (x != NULL) DISPATCH();
1507             break;
1508         }
1509 
1510         TARGET_NOARG(BINARY_SUBTRACT)
1511         {
1512             w = POP();
1513             v = TOP();
1514             if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1515                 /* INLINE: int - int */
1516                 register long a, b, i;
1517                 a = PyInt_AS_LONG(v);
1518                 b = PyInt_AS_LONG(w);
1519                 /* cast to avoid undefined behaviour
1520                    on overflow */
1521                 i = (long)((unsigned long)a - b);
1522                 if ((i^a) < 0 && (i^~b) < 0)
1523                     goto slow_sub;
1524                 x = PyInt_FromLong(i);
1525             }
1526             else {
1527               slow_sub:
1528                 x = PyNumber_Subtract(v, w);
1529             }
1530             Py_DECREF(v);
1531             Py_DECREF(w);
1532             SET_TOP(x);
1533             if (x != NULL) DISPATCH();
1534             break;
1535         }
1536 
1537         TARGET_NOARG(BINARY_SUBSCR)
1538         {
1539             w = POP();
1540             v = TOP();
1541             if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
1542                 /* INLINE: list[int] */
1543                 Py_ssize_t i = PyInt_AsSsize_t(w);
1544                 if (i < 0)
1545                     i += PyList_GET_SIZE(v);
1546                 if (i >= 0 && i < PyList_GET_SIZE(v)) {
1547                     x = PyList_GET_ITEM(v, i);
1548                     Py_INCREF(x);
1549                 }
1550                 else
1551                     goto slow_get;
1552             }
1553             else
1554               slow_get:
1555                 x = PyObject_GetItem(v, w);
1556             Py_DECREF(v);
1557             Py_DECREF(w);
1558             SET_TOP(x);
1559             if (x != NULL) DISPATCH();
1560             break;
1561         }
1562 
1563         TARGET_NOARG(BINARY_LSHIFT)
1564         {
1565             w = POP();
1566             v = TOP();
1567             x = PyNumber_Lshift(v, w);
1568             Py_DECREF(v);
1569             Py_DECREF(w);
1570             SET_TOP(x);
1571             if (x != NULL) DISPATCH();
1572             break;
1573         }
1574 
1575         TARGET_NOARG(BINARY_RSHIFT)
1576         {
1577             w = POP();
1578             v = TOP();
1579             x = PyNumber_Rshift(v, w);
1580             Py_DECREF(v);
1581             Py_DECREF(w);
1582             SET_TOP(x);
1583             if (x != NULL) DISPATCH();
1584             break;
1585         }
1586 
1587         TARGET_NOARG(BINARY_AND)
1588         {
1589             w = POP();
1590             v = TOP();
1591             x = PyNumber_And(v, w);
1592             Py_DECREF(v);
1593             Py_DECREF(w);
1594             SET_TOP(x);
1595             if (x != NULL) DISPATCH();
1596             break;
1597         }
1598 
1599         TARGET_NOARG(BINARY_XOR)
1600         {
1601             w = POP();
1602             v = TOP();
1603             x = PyNumber_Xor(v, w);
1604             Py_DECREF(v);
1605             Py_DECREF(w);
1606             SET_TOP(x);
1607             if (x != NULL) DISPATCH();
1608             break;
1609         }
1610 
1611         TARGET_NOARG(BINARY_OR)
1612         {
1613             w = POP();
1614             v = TOP();
1615             x = PyNumber_Or(v, w);
1616             Py_DECREF(v);
1617             Py_DECREF(w);
1618             SET_TOP(x);
1619             if (x != NULL) DISPATCH();
1620             break;
1621         }
1622 
1623         TARGET(LIST_APPEND)
1624         {
1625             w = POP();
1626             v = PEEK(oparg);
1627             err = PyList_Append(v, w);
1628             Py_DECREF(w);
1629             if (err == 0) {
1630                 PREDICT(JUMP_ABSOLUTE);
1631                 DISPATCH();
1632             }
1633             break;
1634         }
1635 
1636         TARGET(SET_ADD)
1637         {
1638             w = POP();
1639             v = stack_pointer[-oparg];
1640             err = PySet_Add(v, w);
1641             Py_DECREF(w);
1642             if (err == 0) {
1643                 PREDICT(JUMP_ABSOLUTE);
1644                 DISPATCH();
1645             }
1646             break;
1647         }
1648 
1649         TARGET_NOARG(INPLACE_POWER)
1650         {
1651             w = POP();
1652             v = TOP();
1653             x = PyNumber_InPlacePower(v, w, Py_None);
1654             Py_DECREF(v);
1655             Py_DECREF(w);
1656             SET_TOP(x);
1657             if (x != NULL) DISPATCH();
1658             break;
1659         }
1660 
1661         TARGET_NOARG(INPLACE_MULTIPLY)
1662         {
1663             w = POP();
1664             v = TOP();
1665             x = PyNumber_InPlaceMultiply(v, w);
1666             Py_DECREF(v);
1667             Py_DECREF(w);
1668             SET_TOP(x);
1669             if (x != NULL) DISPATCH();
1670             break;
1671         }
1672 
1673         TARGET_NOARG(INPLACE_DIVIDE)
1674         {
1675             if (!_Py_QnewFlag) {
1676                 w = POP();
1677                 v = TOP();
1678                 x = PyNumber_InPlaceDivide(v, w);
1679                 Py_DECREF(v);
1680                 Py_DECREF(w);
1681                 SET_TOP(x);
1682                 if (x != NULL) DISPATCH();
1683                 break;
1684             }
1685         }
1686             /* -Qnew is in effect:  fall through to
1687                INPLACE_TRUE_DIVIDE */
1688         TARGET_NOARG(INPLACE_TRUE_DIVIDE)
1689         {
1690             w = POP();
1691             v = TOP();
1692             x = PyNumber_InPlaceTrueDivide(v, w);
1693             Py_DECREF(v);
1694             Py_DECREF(w);
1695             SET_TOP(x);
1696             if (x != NULL) DISPATCH();
1697             break;
1698         }
1699 
1700         TARGET_NOARG(INPLACE_FLOOR_DIVIDE)
1701         {
1702             w = POP();
1703             v = TOP();
1704             x = PyNumber_InPlaceFloorDivide(v, w);
1705             Py_DECREF(v);
1706             Py_DECREF(w);
1707             SET_TOP(x);
1708             if (x != NULL) DISPATCH();
1709             break;
1710         }
1711 
1712         TARGET_NOARG(INPLACE_MODULO)
1713         {
1714             w = POP();
1715             v = TOP();
1716             x = PyNumber_InPlaceRemainder(v, w);
1717             Py_DECREF(v);
1718             Py_DECREF(w);
1719             SET_TOP(x);
1720             if (x != NULL) DISPATCH();
1721             break;
1722         }
1723 
1724         TARGET_NOARG(INPLACE_ADD)
1725         {
1726             w = POP();
1727             v = TOP();
1728             if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1729                 /* INLINE: int + int */
1730                 register long a, b, i;
1731                 a = PyInt_AS_LONG(v);
1732                 b = PyInt_AS_LONG(w);
1733                 i = a + b;
1734                 if ((i^a) < 0 && (i^b) < 0)
1735                     goto slow_iadd;
1736                 x = PyInt_FromLong(i);
1737             }
1738             else if (PyString_CheckExact(v) &&
1739                      PyString_CheckExact(w)) {
1740                 x = string_concatenate(v, w, f, next_instr);
1741                 /* string_concatenate consumed the ref to v */
1742                 goto skip_decref_v;
1743             }
1744             else {
1745               slow_iadd:
1746                 x = PyNumber_InPlaceAdd(v, w);
1747             }
1748             Py_DECREF(v);
1749           skip_decref_v:
1750             Py_DECREF(w);
1751             SET_TOP(x);
1752             if (x != NULL) DISPATCH();
1753             break;
1754         }
1755 
1756         TARGET_NOARG(INPLACE_SUBTRACT)
1757         {
1758             w = POP();
1759             v = TOP();
1760             if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1761                 /* INLINE: int - int */
1762                 register long a, b, i;
1763                 a = PyInt_AS_LONG(v);
1764                 b = PyInt_AS_LONG(w);
1765                 i = a - b;
1766                 if ((i^a) < 0 && (i^~b) < 0)
1767                     goto slow_isub;
1768                 x = PyInt_FromLong(i);
1769             }
1770             else {
1771               slow_isub:
1772                 x = PyNumber_InPlaceSubtract(v, w);
1773             }
1774             Py_DECREF(v);
1775             Py_DECREF(w);
1776             SET_TOP(x);
1777             if (x != NULL) DISPATCH();
1778             break;
1779         }
1780 
1781         TARGET_NOARG(INPLACE_LSHIFT)
1782         {
1783             w = POP();
1784             v = TOP();
1785             x = PyNumber_InPlaceLshift(v, w);
1786             Py_DECREF(v);
1787             Py_DECREF(w);
1788             SET_TOP(x);
1789             if (x != NULL) DISPATCH();
1790             break;
1791         }
1792 
1793         TARGET_NOARG(INPLACE_RSHIFT)
1794         {
1795             w = POP();
1796             v = TOP();
1797             x = PyNumber_InPlaceRshift(v, w);
1798             Py_DECREF(v);
1799             Py_DECREF(w);
1800             SET_TOP(x);
1801             if (x != NULL) DISPATCH();
1802             break;
1803         }
1804 
1805         TARGET_NOARG(INPLACE_AND)
1806         {
1807             w = POP();
1808             v = TOP();
1809             x = PyNumber_InPlaceAnd(v, w);
1810             Py_DECREF(v);
1811             Py_DECREF(w);
1812             SET_TOP(x);
1813             if (x != NULL) DISPATCH();
1814             break;
1815         }
1816 
1817         TARGET_NOARG(INPLACE_XOR)
1818         {
1819             w = POP();
1820             v = TOP();
1821             x = PyNumber_InPlaceXor(v, w);
1822             Py_DECREF(v);
1823             Py_DECREF(w);
1824             SET_TOP(x);
1825             if (x != NULL) DISPATCH();
1826             break;
1827         }
1828 
1829         TARGET_NOARG(INPLACE_OR)
1830         {
1831             w = POP();
1832             v = TOP();
1833             x = PyNumber_InPlaceOr(v, w);
1834             Py_DECREF(v);
1835             Py_DECREF(w);
1836             SET_TOP(x);
1837             if (x != NULL) DISPATCH();
1838             break;
1839         }
1840 
1841 
1842 
1843         TARGET_WITH_IMPL_NOARG(SLICE, _slice)
1844         TARGET_WITH_IMPL_NOARG(SLICE_1, _slice)
1845         TARGET_WITH_IMPL_NOARG(SLICE_2, _slice)
1846         TARGET_WITH_IMPL_NOARG(SLICE_3, _slice)
1847         _slice:
1848         {
1849             if ((opcode-SLICE) & 2)
1850                 w = POP();
1851             else
1852                 w = NULL;
1853             if ((opcode-SLICE) & 1)
1854                 v = POP();
1855             else
1856                 v = NULL;
1857             u = TOP();
1858             x = apply_slice(u, v, w);
1859             Py_DECREF(u);
1860             Py_XDECREF(v);
1861             Py_XDECREF(w);
1862             SET_TOP(x);
1863             if (x != NULL) DISPATCH();
1864             break;
1865         }
1866 
1867 
1868         TARGET_WITH_IMPL_NOARG(STORE_SLICE, _store_slice)
1869         TARGET_WITH_IMPL_NOARG(STORE_SLICE_1, _store_slice)
1870         TARGET_WITH_IMPL_NOARG(STORE_SLICE_2, _store_slice)
1871         TARGET_WITH_IMPL_NOARG(STORE_SLICE_3, _store_slice)
1872         _store_slice:
1873         {
1874             if ((opcode-STORE_SLICE) & 2)
1875                 w = POP();
1876             else
1877                 w = NULL;
1878             if ((opcode-STORE_SLICE) & 1)
1879                 v = POP();
1880             else
1881                 v = NULL;
1882             u = POP();
1883             t = POP();
1884             err = assign_slice(u, v, w, t); /* u[v:w] = t */
1885             Py_DECREF(t);
1886             Py_DECREF(u);
1887             Py_XDECREF(v);
1888             Py_XDECREF(w);
1889             if (err == 0) DISPATCH();
1890             break;
1891         }
1892 
1893 
1894         TARGET_WITH_IMPL_NOARG(DELETE_SLICE, _delete_slice)
1895         TARGET_WITH_IMPL_NOARG(DELETE_SLICE_1, _delete_slice)
1896         TARGET_WITH_IMPL_NOARG(DELETE_SLICE_2, _delete_slice)
1897         TARGET_WITH_IMPL_NOARG(DELETE_SLICE_3, _delete_slice)
1898         _delete_slice:
1899         {
1900             if ((opcode-DELETE_SLICE) & 2)
1901                 w = POP();
1902             else
1903                 w = NULL;
1904             if ((opcode-DELETE_SLICE) & 1)
1905                 v = POP();
1906             else
1907                 v = NULL;
1908             u = POP();
1909             err = assign_slice(u, v, w, (PyObject *)NULL);
1910                                             /* del u[v:w] */
1911             Py_DECREF(u);
1912             Py_XDECREF(v);
1913             Py_XDECREF(w);
1914             if (err == 0) DISPATCH();
1915             break;
1916         }
1917 
1918         TARGET_NOARG(STORE_SUBSCR)
1919         {
1920             w = TOP();
1921             v = SECOND();
1922             u = THIRD();
1923             STACKADJ(-3);
1924             /* v[w] = u */
1925             err = PyObject_SetItem(v, w, u);
1926             Py_DECREF(u);
1927             Py_DECREF(v);
1928             Py_DECREF(w);
1929             if (err == 0) DISPATCH();
1930             break;
1931         }
1932 
1933         TARGET_NOARG(DELETE_SUBSCR)
1934         {
1935             w = TOP();
1936             v = SECOND();
1937             STACKADJ(-2);
1938             /* del v[w] */
1939             err = PyObject_DelItem(v, w);
1940             Py_DECREF(v);
1941             Py_DECREF(w);
1942             if (err == 0) DISPATCH();
1943             break;
1944         }
1945 
1946         TARGET_NOARG(PRINT_EXPR)
1947         {
1948             v = POP();
1949             w = PySys_GetObject("displayhook");
1950             if (w == NULL) {
1951                 PyErr_SetString(PyExc_RuntimeError,
1952                                 "lost sys.displayhook");
1953                 err = -1;
1954                 x = NULL;
1955             }
1956             if (err == 0) {
1957                 x = PyTuple_Pack(1, v);
1958                 if (x == NULL)
1959                     err = -1;
1960             }
1961             if (err == 0) {
1962                 w = PyEval_CallObject(w, x);
1963                 Py_XDECREF(w);
1964                 if (w == NULL)
1965                     err = -1;
1966             }
1967             Py_DECREF(v);
1968             Py_XDECREF(x);
1969             break;
1970         }
1971 
1972         TARGET_NOARG(PRINT_ITEM_TO)
1973         {
1974             w = stream = POP();
1975             /* fall through to PRINT_ITEM */
1976         }
1977 
1978         TARGET_NOARG(PRINT_ITEM)
1979         {
1980             v = POP();
1981             if (stream == NULL || stream == Py_None) {
1982                 w = PySys_GetObject("stdout");
1983                 if (w == NULL) {
1984                     PyErr_SetString(PyExc_RuntimeError,
1985                                     "lost sys.stdout");
1986                     err = -1;
1987                 }
1988             }
1989             /* PyFile_SoftSpace() can exececute arbitrary code
1990                if sys.stdout is an instance with a __getattr__.
1991                If __getattr__ raises an exception, w will
1992                be freed, so we need to prevent that temporarily. */
1993             Py_XINCREF(w);
1994             if (w != NULL && PyFile_SoftSpace(w, 0))
1995                 err = PyFile_WriteString(" ", w);
1996             if (err == 0)
1997                 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
1998             if (err == 0) {
1999                 /* XXX move into writeobject() ? */
2000                 if (PyString_Check(v)) {
2001                     char *s = PyString_AS_STRING(v);
2002                     Py_ssize_t len = PyString_GET_SIZE(v);
2003                     if (len == 0 ||
2004                         !isspace(Py_CHARMASK(s[len-1])) ||
2005                         s[len-1] == ' ')
2006                         PyFile_SoftSpace(w, 1);
2007                 }
2008 #ifdef Py_USING_UNICODE
2009                 else if (PyUnicode_Check(v)) {
2010                     Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
2011                     Py_ssize_t len = PyUnicode_GET_SIZE(v);
2012                     if (len == 0 ||
2013                         !Py_UNICODE_ISSPACE(s[len-1]) ||
2014                         s[len-1] == ' ')
2015                         PyFile_SoftSpace(w, 1);
2016                 }
2017 #endif
2018                 else
2019                     PyFile_SoftSpace(w, 1);
2020             }
2021             Py_XDECREF(w);
2022             Py_DECREF(v);
2023             Py_XDECREF(stream);
2024             stream = NULL;
2025             if (err == 0) DISPATCH();
2026             break;
2027         }
2028 
2029         TARGET_NOARG(PRINT_NEWLINE_TO)
2030         {
2031             w = stream = POP();
2032             /* fall through to PRINT_NEWLINE */
2033         }
2034 
2035         TARGET_NOARG(PRINT_NEWLINE)
2036         {
2037             if (stream == NULL || stream == Py_None)
2038             {
2039                 w = PySys_GetObject("stdout");
2040                 if (w == NULL) {
2041                     PyErr_SetString(PyExc_RuntimeError,
2042                                     "lost sys.stdout");
2043                     why = WHY_EXCEPTION;
2044                 }
2045             }
2046             if (w != NULL) {
2047                 /* w.write() may replace sys.stdout, so we
2048                  * have to keep our reference to it */
2049                 Py_INCREF(w);
2050                 err = PyFile_WriteString("\n", w);
2051                 if (err == 0)
2052                     PyFile_SoftSpace(w, 0);
2053                 Py_DECREF(w);
2054             }
2055             Py_XDECREF(stream);
2056             stream = NULL;
2057             break;
2058         }
2059 
2060 #ifdef CASE_TOO_BIG
2061         default: switch (opcode) {
2062 #endif
2063 
2064         TARGET(RAISE_VARARGS)
2065             {
2066             u = v = w = NULL;
2067             switch (oparg) {
2068             case 3:
2069                 u = POP(); /* traceback */
2070                 /* Fallthrough */
2071             case 2:
2072                 v = POP(); /* value */
2073                 /* Fallthrough */
2074             case 1:
2075                 w = POP(); /* exc */
2076             case 0: /* Fallthrough */
2077                 why = do_raise(w, v, u);
2078                 break;
2079             default:
2080                 PyErr_SetString(PyExc_SystemError,
2081                            "bad RAISE_VARARGS oparg");
2082                 why = WHY_EXCEPTION;
2083                 break;
2084             }
2085             break;
2086             }
2087 
2088         TARGET_NOARG(LOAD_LOCALS)
2089         {
2090             if ((x = f->f_locals) != NULL)
2091             {
2092                 Py_INCREF(x);
2093                 PUSH(x);
2094                 DISPATCH();
2095             }
2096             PyErr_SetString(PyExc_SystemError, "no locals");
2097             break;
2098         }
2099 
2100         TARGET_NOARG(RETURN_VALUE)
2101         {
2102             retval = POP();
2103             why = WHY_RETURN;
2104             goto fast_block_end;
2105         }
2106 
2107         TARGET_NOARG(YIELD_VALUE)
2108         {
2109             retval = POP();
2110             f->f_stacktop = stack_pointer;
2111             why = WHY_YIELD;
2112             goto fast_yield;
2113         }
2114 
2115         TARGET_NOARG(EXEC_STMT)
2116         {
2117             w = TOP();
2118             v = SECOND();
2119             u = THIRD();
2120             STACKADJ(-3);
2121             READ_TIMESTAMP(intr0);
2122             err = exec_statement(f, u, v, w);
2123             READ_TIMESTAMP(intr1);
2124             Py_DECREF(u);
2125             Py_DECREF(v);
2126             Py_DECREF(w);
2127             break;
2128         }
2129 
2130         TARGET_NOARG(POP_BLOCK)
2131         {
2132             {
2133                 PyTryBlock *b = PyFrame_BlockPop(f);
2134                 while (STACK_LEVEL() > b->b_level) {
2135                     v = POP();
2136                     Py_DECREF(v);
2137                 }
2138             }
2139             DISPATCH();
2140         }
2141 
2142         PREDICTED(END_FINALLY);
2143         TARGET_NOARG(END_FINALLY)
2144         {
2145             v = POP();
2146             if (PyInt_Check(v)) {
2147                 why = (enum why_code) PyInt_AS_LONG(v);
2148                 assert(why != WHY_YIELD);
2149                 if (why == WHY_RETURN ||
2150                     why == WHY_CONTINUE)
2151                     retval = POP();
2152             }
2153             else if (PyExceptionClass_Check(v) ||
2154                      PyString_Check(v)) {
2155                 w = POP();
2156                 u = POP();
2157                 PyErr_Restore(v, w, u);
2158                 why = WHY_RERAISE;
2159                 break;
2160             }
2161             else if (v != Py_None) {
2162                 PyErr_SetString(PyExc_SystemError,
2163                     "'finally' pops bad exception");
2164                 why = WHY_EXCEPTION;
2165             }
2166             Py_DECREF(v);
2167             break;
2168         }
2169 
2170         TARGET_NOARG(BUILD_CLASS)
2171         {
2172             u = TOP();
2173             v = SECOND();
2174             w = THIRD();
2175             STACKADJ(-2);
2176             x = build_class(u, v, w);
2177             SET_TOP(x);
2178             Py_DECREF(u);
2179             Py_DECREF(v);
2180             Py_DECREF(w);
2181             break;
2182         }
2183 
2184         TARGET(STORE_NAME)
2185         {
2186             w = GETITEM(names, oparg);
2187             v = POP();
2188             if ((x = f->f_locals) != NULL) {
2189                 if (PyDict_CheckExact(x))
2190                     err = PyDict_SetItem(x, w, v);
2191                 else
2192                     err = PyObject_SetItem(x, w, v);
2193                 Py_DECREF(v);
2194                 if (err == 0) DISPATCH();
2195                 break;
2196             }
2197             t = PyObject_Repr(w);
2198             if (t == NULL)
2199                 break;
2200             PyErr_Format(PyExc_SystemError,
2201                          "no locals found when storing %s",
2202                          PyString_AS_STRING(t));
2203             Py_DECREF(t);
2204             break;
2205         }
2206 
2207         TARGET(DELETE_NAME)
2208         {
2209             w = GETITEM(names, oparg);
2210             if ((x = f->f_locals) != NULL) {
2211                 if ((err = PyObject_DelItem(x, w)) != 0)
2212                     format_exc_check_arg(PyExc_NameError,
2213                                          NAME_ERROR_MSG,
2214                                          w);
2215                 break;
2216             }
2217             t = PyObject_Repr(w);
2218             if (t == NULL)
2219                 break;
2220             PyErr_Format(PyExc_SystemError,
2221                          "no locals when deleting %s",
2222                          PyString_AS_STRING(w));
2223             Py_DECREF(t);
2224             break;
2225         }
2226 
2227         PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
2228         TARGET(UNPACK_SEQUENCE)
2229         {
2230             v = POP();
2231             if (PyTuple_CheckExact(v) &&
2232                 PyTuple_GET_SIZE(v) == oparg) {
2233                 PyObject **items = \
2234                     ((PyTupleObject *)v)->ob_item;
2235                 while (oparg--) {
2236                     w = items[oparg];
2237                     Py_INCREF(w);
2238                     PUSH(w);
2239                 }
2240                 Py_DECREF(v);
2241                 DISPATCH();
2242             } else if (PyList_CheckExact(v) &&
2243                        PyList_GET_SIZE(v) == oparg) {
2244                 PyObject **items = \
2245                     ((PyListObject *)v)->ob_item;
2246                 while (oparg--) {
2247                     w = items[oparg];
2248                     Py_INCREF(w);
2249                     PUSH(w);
2250                 }
2251             } else if (unpack_iterable(v, oparg,
2252                                        stack_pointer + oparg)) {
2253                 STACKADJ(oparg);
2254             } else {
2255                 /* unpack_iterable() raised an exception */
2256                 why = WHY_EXCEPTION;
2257             }
2258             Py_DECREF(v);
2259             break;
2260         }
2261 
2262 
2263         TARGET(STORE_ATTR)
2264         {
2265             w = GETITEM(names, oparg);
2266             v = TOP();
2267             u = SECOND();
2268             STACKADJ(-2);
2269             err = PyObject_SetAttr(v, w, u); /* v.w = u */
2270             Py_DECREF(v);
2271             Py_DECREF(u);
2272             if (err == 0) DISPATCH();
2273             break;
2274         }
2275 
2276         TARGET(DELETE_ATTR)
2277         {
2278             w = GETITEM(names, oparg);
2279             v = POP();
2280             err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2281                                             /* del v.w */
2282             Py_DECREF(v);
2283             break;
2284         }
2285 
2286 
2287         TARGET(STORE_GLOBAL)
2288         {
2289             w = GETITEM(names, oparg);
2290             v = POP();
2291             err = PyDict_SetItem(f->f_globals, w, v);
2292             Py_DECREF(v);
2293             if (err == 0) DISPATCH();
2294             break;
2295         }
2296 
2297         TARGET(DELETE_GLOBAL)
2298         {
2299             w = GETITEM(names, oparg);
2300             if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2301                 format_exc_check_arg(
2302                     PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2303             break;
2304         }
2305 
2306         TARGET(LOAD_NAME)
2307         {
2308             w = GETITEM(names, oparg);
2309             if ((v = f->f_locals) == NULL) {
2310                 why = WHY_EXCEPTION;
2311                 t = PyObject_Repr(w);
2312                 if (t == NULL)
2313                     break;
2314                 PyErr_Format(PyExc_SystemError,
2315                              "no locals when loading %s",
2316                              PyString_AS_STRING(w));
2317                 Py_DECREF(t);
2318                 break;
2319             }
2320             if (PyDict_CheckExact(v)) {
2321                 x = PyDict_GetItem(v, w);
2322                 Py_XINCREF(x);
2323             }
2324             else {
2325                 x = PyObject_GetItem(v, w);
2326                 if (x == NULL && PyErr_Occurred()) {
2327                     if (!PyErr_ExceptionMatches(
2328                                     PyExc_KeyError))
2329                         break;
2330                     PyErr_Clear();
2331                 }
2332             }
2333             if (x == NULL) {
2334                 x = PyDict_GetItem(f->f_globals, w);
2335                 if (x == NULL) {
2336                     x = PyDict_GetItem(f->f_builtins, w);
2337                     if (x == NULL) {
2338                         format_exc_check_arg(
2339                                     PyExc_NameError,
2340                                     NAME_ERROR_MSG, w);
2341                         break;
2342                     }
2343                 }
2344                 Py_INCREF(x);
2345             }
2346             PUSH(x);
2347             DISPATCH();
2348         }
2349 
2350         TARGET(LOAD_GLOBAL)
2351         {
2352             w = GETITEM(names, oparg);
2353             if (PyString_CheckExact(w)) {
2354                 /* Inline the PyDict_GetItem() calls.
2355                    WARNING: this is an extreme speed hack.
2356                    Do not try this at home. */
2357                 long hash = ((PyStringObject *)w)->ob_shash;
2358                 if (hash != -1) {
2359                     PyDictObject *d;
2360                     PyDictEntry *e;
2361                     d = (PyDictObject *)(f->f_globals);
2362                     e = d->ma_lookup(d, w, hash);
2363                     if (e == NULL) {
2364                         x = NULL;
2365                         break;
2366                     }
2367                     x = e->me_value;
2368                     if (x != NULL) {
2369                         Py_INCREF(x);
2370                         PUSH(x);
2371                         DISPATCH();
2372                     }
2373                     d = (PyDictObject *)(f->f_builtins);
2374                     e = d->ma_lookup(d, w, hash);
2375                     if (e == NULL) {
2376                         x = NULL;
2377                         break;
2378                     }
2379                     x = e->me_value;
2380                     if (x != NULL) {
2381                         Py_INCREF(x);
2382                         PUSH(x);
2383                         DISPATCH();
2384                     }
2385                     goto load_global_error;
2386                 }
2387             }
2388             /* This is the un-inlined version of the code above */
2389             x = PyDict_GetItem(f->f_globals, w);
2390             if (x == NULL) {
2391                 x = PyDict_GetItem(f->f_builtins, w);
2392                 if (x == NULL) {
2393                   load_global_error:
2394                     format_exc_check_arg(
2395                                 PyExc_NameError,
2396                                 GLOBAL_NAME_ERROR_MSG, w);
2397                     break;
2398                 }
2399             }
2400             Py_INCREF(x);
2401             PUSH(x);
2402             DISPATCH();
2403         }
2404 
2405         TARGET(DELETE_FAST)
2406         {
2407             x = GETLOCAL(oparg);
2408             if (x != NULL) {
2409                 SETLOCAL(oparg, NULL);
2410                 DISPATCH();
2411             }
2412             format_exc_check_arg(
2413                 PyExc_UnboundLocalError,
2414                 UNBOUNDLOCAL_ERROR_MSG,
2415                 PyTuple_GetItem(co->co_varnames, oparg)
2416                 );
2417             break;
2418         }
2419 
2420         TARGET(LOAD_CLOSURE)
2421         {
2422             x = freevars[oparg];
2423             Py_INCREF(x);
2424             PUSH(x);
2425             if (x != NULL) DISPATCH();
2426             break;
2427         }
2428 
2429         TARGET(LOAD_DEREF)
2430         {
2431             x = freevars[oparg];
2432             w = PyCell_Get(x);
2433             if (w != NULL) {
2434                 PUSH(w);
2435                 DISPATCH();
2436             }
2437             err = -1;
2438             /* Don't stomp existing exception */
2439             if (PyErr_Occurred())
2440                 break;
2441             if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2442                 v = PyTuple_GET_ITEM(co->co_cellvars,
2443                                      oparg);
2444                 format_exc_check_arg(
2445                        PyExc_UnboundLocalError,
2446                        UNBOUNDLOCAL_ERROR_MSG,
2447                        v);
2448             } else {
2449                 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2450                     PyTuple_GET_SIZE(co->co_cellvars));
2451                 format_exc_check_arg(PyExc_NameError,
2452                                      UNBOUNDFREE_ERROR_MSG, v);
2453             }
2454             break;
2455         }
2456 
2457         TARGET(STORE_DEREF)
2458         {
2459             w = POP();
2460             x = freevars[oparg];
2461             PyCell_Set(x, w);
2462             Py_DECREF(w);
2463             DISPATCH();
2464         }
2465 
2466         TARGET(BUILD_TUPLE)
2467         {
2468             x = PyTuple_New(oparg);
2469             if (x != NULL) {
2470                 for (; --oparg >= 0;) {
2471                     w = POP();
2472                     PyTuple_SET_ITEM(x, oparg, w);
2473                 }
2474                 PUSH(x);
2475                 DISPATCH();
2476             }
2477             break;
2478         }
2479 
2480         TARGET(BUILD_LIST)
2481         {
2482             x =  PyList_New(oparg);
2483             if (x != NULL) {
2484                 for (; --oparg >= 0;) {
2485                     w = POP();
2486                     PyList_SET_ITEM(x, oparg, w);
2487                 }
2488                 PUSH(x);
2489                 DISPATCH();
2490             }
2491             break;
2492         }
2493 
2494         TARGET(BUILD_SET)
2495         {
2496             int i;
2497             x = PySet_New(NULL);
2498             if (x != NULL) {
2499                 for (i = oparg; i > 0; i--) {
2500                     w = PEEK(i);
2501                     if (err == 0)
2502                         err = PySet_Add(x, w);
2503                     Py_DECREF(w);
2504                 }
2505                 STACKADJ(-oparg);
2506                 if (err != 0) {
2507                     Py_DECREF(x);
2508                     break;
2509                 }
2510                 PUSH(x);
2511                 DISPATCH();
2512             }
2513             break;
2514         }
2515 
2516         TARGET(BUILD_MAP)
2517         {
2518             x = _PyDict_NewPresized((Py_ssize_t)oparg);
2519             PUSH(x);
2520             if (x != NULL) DISPATCH();
2521             break;
2522         }
2523 
2524         TARGET_NOARG(STORE_MAP)
2525         {
2526             w = TOP();     /* key */
2527             u = SECOND();  /* value */
2528             v = THIRD();   /* dict */
2529             STACKADJ(-2);
2530             assert (PyDict_CheckExact(v));
2531             err = PyDict_SetItem(v, w, u);  /* v[w] = u */
2532             Py_DECREF(u);
2533             Py_DECREF(w);
2534             if (err == 0) DISPATCH();
2535             break;
2536         }
2537 
2538         TARGET(MAP_ADD)
2539         {
2540             w = TOP();     /* key */
2541             u = SECOND();  /* value */
2542             STACKADJ(-2);
2543             v = stack_pointer[-oparg];  /* dict */
2544             assert (PyDict_CheckExact(v));
2545             err = PyDict_SetItem(v, w, u);  /* v[w] = u */
2546             Py_DECREF(u);
2547             Py_DECREF(w);
2548             if (err == 0) {
2549                 PREDICT(JUMP_ABSOLUTE);
2550                 DISPATCH();
2551             }
2552             break;
2553         }
2554 
2555         TARGET(LOAD_ATTR)
2556         {
2557             w = GETITEM(names, oparg);
2558             v = TOP();
2559             x = PyObject_GetAttr(v, w);
2560             Py_DECREF(v);
2561             SET_TOP(x);
2562             if (x != NULL) DISPATCH();
2563             break;
2564         }
2565 
2566         TARGET(COMPARE_OP)
2567         {
2568             w = POP();
2569             v = TOP();
2570             if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
2571                 /* INLINE: cmp(int, int) */
2572                 register long a, b;
2573                 register int res;
2574                 a = PyInt_AS_LONG(v);
2575                 b = PyInt_AS_LONG(w);
2576                 switch (oparg) {
2577                 case PyCmp_LT: res = a <  b; break;
2578                 case PyCmp_LE: res = a <= b; break;
2579                 case PyCmp_EQ: res = a == b; break;
2580                 case PyCmp_NE: res = a != b; break;
2581                 case PyCmp_GT: res = a >  b; break;
2582                 case PyCmp_GE: res = a >= b; break;
2583                 case PyCmp_IS: res = v == w; break;
2584                 case PyCmp_IS_NOT: res = v != w; break;
2585                 default: goto slow_compare;
2586                 }
2587                 x = res ? Py_True : Py_False;
2588                 Py_INCREF(x);
2589             }
2590             else {
2591               slow_compare:
2592                 x = cmp_outcome(oparg, v, w);
2593             }
2594             Py_DECREF(v);
2595             Py_DECREF(w);
2596             SET_TOP(x);
2597             if (x == NULL) break;
2598             PREDICT(POP_JUMP_IF_FALSE);
2599             PREDICT(POP_JUMP_IF_TRUE);
2600             DISPATCH();
2601         }
2602 
2603         TARGET(IMPORT_NAME)
2604         {
2605             long res;
2606             w = GETITEM(names, oparg);
2607             x = PyDict_GetItemString(f->f_builtins, "__import__");
2608             if (x == NULL) {
2609                 PyErr_SetString(PyExc_ImportError,
2610                                 "__import__ not found");
2611                 break;
2612             }
2613             Py_INCREF(x);
2614             v = POP();
2615             u = TOP();
2616             res = PyInt_AsLong(u);
2617             if (res != -1 || PyErr_Occurred()) {
2618                 if (res == -1) {
2619                     assert(PyErr_Occurred());
2620                     PyErr_Clear();
2621                 }
2622                 w = PyTuple_Pack(5,
2623                             w,
2624                             f->f_globals,
2625                             f->f_locals == NULL ?
2626                                   Py_None : f->f_locals,
2627                             v,
2628                             u);
2629             }
2630             else
2631                 w = PyTuple_Pack(4,
2632                             w,
2633                             f->f_globals,
2634                             f->f_locals == NULL ?
2635                                   Py_None : f->f_locals,
2636                             v);
2637             Py_DECREF(v);
2638             Py_DECREF(u);
2639             if (w == NULL) {
2640                 u = POP();
2641                 Py_DECREF(x);
2642                 x = NULL;
2643                 break;
2644             }
2645             READ_TIMESTAMP(intr0);
2646             v = x;
2647             x = PyEval_CallObject(v, w);
2648             Py_DECREF(v);
2649             READ_TIMESTAMP(intr1);
2650             Py_DECREF(w);
2651             SET_TOP(x);
2652             if (x != NULL) DISPATCH();
2653             break;
2654         }
2655 
2656         TARGET_NOARG(IMPORT_STAR)
2657         {
2658             v = POP();
2659             PyFrame_FastToLocals(f);
2660             if ((x = f->f_locals) == NULL) {
2661                 PyErr_SetString(PyExc_SystemError,
2662                     "no locals found during 'import *'");
2663                 Py_DECREF(v);
2664                 break;
2665             }
2666             READ_TIMESTAMP(intr0);
2667             err = import_all_from(x, v);
2668             READ_TIMESTAMP(intr1);
2669             PyFrame_LocalsToFast(f, 0);
2670             Py_DECREF(v);
2671             if (err == 0) DISPATCH();
2672             break;
2673         }
2674 
2675         TARGET(IMPORT_FROM)
2676         {
2677             w = GETITEM(names, oparg);
2678             v = TOP();
2679             READ_TIMESTAMP(intr0);
2680             x = import_from(v, w);
2681             READ_TIMESTAMP(intr1);
2682             PUSH(x);
2683             if (x != NULL) DISPATCH();
2684             break;
2685         }
2686 
2687         TARGET(JUMP_FORWARD)
2688         {
2689             JUMPBY(oparg);
2690             FAST_DISPATCH();
2691         }
2692 
2693         PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2694         TARGET(POP_JUMP_IF_FALSE)
2695         {
2696             w = POP();
2697             if (w == Py_True) {
2698                 Py_DECREF(w);
2699                 FAST_DISPATCH();
2700             }
2701             if (w == Py_False) {
2702                 Py_DECREF(w);
2703                 JUMPTO(oparg);
2704                 FAST_DISPATCH();
2705             }
2706             err = PyObject_IsTrue(w);
2707             Py_DECREF(w);
2708             if (err > 0)
2709                 err = 0;
2710             else if (err == 0)
2711                 JUMPTO(oparg);
2712             else
2713                 break;
2714             DISPATCH();
2715         }
2716 
2717         PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2718         TARGET(POP_JUMP_IF_TRUE)
2719         {
2720             w = POP();
2721             if (w == Py_False) {
2722                 Py_DECREF(w);
2723                 FAST_DISPATCH();
2724             }
2725             if (w == Py_True) {
2726                 Py_DECREF(w);
2727                 JUMPTO(oparg);
2728                 FAST_DISPATCH();
2729             }
2730             err = PyObject_IsTrue(w);
2731             Py_DECREF(w);
2732             if (err > 0) {
2733                 err = 0;
2734                 JUMPTO(oparg);
2735             }
2736             else if (err == 0)
2737                 ;
2738             else
2739                 break;
2740             DISPATCH();
2741         }
2742 
2743         TARGET(JUMP_IF_FALSE_OR_POP)
2744         {
2745             w = TOP();
2746             if (w == Py_True) {
2747                 STACKADJ(-1);
2748                 Py_DECREF(w);
2749                 FAST_DISPATCH();
2750             }
2751             if (w == Py_False) {
2752                 JUMPTO(oparg);
2753                 FAST_DISPATCH();
2754             }
2755             err = PyObject_IsTrue(w);
2756             if (err > 0) {
2757                 STACKADJ(-1);
2758                 Py_DECREF(w);
2759                 err = 0;
2760             }
2761             else if (err == 0)
2762                 JUMPTO(oparg);
2763             else
2764                 break;
2765             DISPATCH();
2766         }
2767 
2768         TARGET(JUMP_IF_TRUE_OR_POP)
2769         {
2770             w = TOP();
2771             if (w == Py_False) {
2772                 STACKADJ(-1);
2773                 Py_DECREF(w);
2774                 FAST_DISPATCH();
2775             }
2776             if (w == Py_True) {
2777                 JUMPTO(oparg);
2778                 FAST_DISPATCH();
2779             }
2780             err = PyObject_IsTrue(w);
2781             if (err > 0) {
2782                 err = 0;
2783                 JUMPTO(oparg);
2784             }
2785             else if (err == 0) {
2786                 STACKADJ(-1);
2787                 Py_DECREF(w);
2788             }
2789             else
2790                 break;
2791             DISPATCH();
2792         }
2793 
2794         PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2795         TARGET(JUMP_ABSOLUTE)
2796         {
2797             JUMPTO(oparg);
2798 #if FAST_LOOPS
2799             /* Enabling this path speeds-up all while and for-loops by bypassing
2800                the per-loop checks for signals.  By default, this should be turned-off
2801                because it prevents detection of a control-break in tight loops like
2802                "while 1: pass".  Compile with this option turned-on when you need
2803                the speed-up and do not need break checking inside tight loops (ones
2804                that contain only instructions ending with goto fast_next_opcode).
2805             */
2806             goto fast_next_opcode;
2807 #else
2808             DISPATCH();
2809 #endif
2810         }
2811 
2812         TARGET_NOARG(GET_ITER)
2813         {
2814             /* before: [obj]; after [getiter(obj)] */
2815             v = TOP();
2816             x = PyObject_GetIter(v);
2817             Py_DECREF(v);
2818             if (x != NULL) {
2819                 SET_TOP(x);
2820                 PREDICT(FOR_ITER);
2821                 DISPATCH();
2822             }
2823             STACKADJ(-1);
2824             break;
2825         }
2826 
2827         PREDICTED_WITH_ARG(FOR_ITER);
2828         TARGET(FOR_ITER)
2829         {
2830             /* before: [iter]; after: [iter, iter()] *or* [] */
2831             v = TOP();
2832             x = (*v->ob_type->tp_iternext)(v);
2833             if (x != NULL) {
2834                 PUSH(x);
2835                 PREDICT(STORE_FAST);
2836                 PREDICT(UNPACK_SEQUENCE);
2837                 DISPATCH();
2838             }
2839             if (PyErr_Occurred()) {
2840                 if (!PyErr_ExceptionMatches(
2841                                 PyExc_StopIteration))
2842                     break;
2843                 PyErr_Clear();
2844             }
2845             /* iterator ended normally */
2846             x = v = POP();
2847             Py_DECREF(v);
2848             JUMPBY(oparg);
2849             DISPATCH();
2850         }
2851 
2852         TARGET_NOARG(BREAK_LOOP)
2853         {
2854             why = WHY_BREAK;
2855             goto fast_block_end;
2856         }
2857 
2858         TARGET(CONTINUE_LOOP)
2859         {
2860             retval = PyInt_FromLong(oparg);
2861             if (!retval) {
2862                 x = NULL;
2863                 break;
2864             }
2865             why = WHY_CONTINUE;
2866             goto fast_block_end;
2867         }
2868 
2869         TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2870         TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2871         TARGET(SETUP_FINALLY)
2872         _setup_finally:
2873         {
2874             /* NOTE: If you add any new block-setup opcodes that
2875                are not try/except/finally handlers, you may need
2876                to update the PyGen_NeedsFinalizing() function.
2877                */
2878 
2879             PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2880                                STACK_LEVEL());
2881             DISPATCH();
2882         }
2883 
2884 
2885 
2886         TARGET(SETUP_WITH)
2887         {
2888         {
2889             static PyObject *exit, *enter;
2890             w = TOP();
2891             x = special_lookup(w, "__exit__", &exit);
2892             if (!x)
2893                 break;
2894             SET_TOP(x);
2895             u = special_lookup(w, "__enter__", &enter);
2896             Py_DECREF(w);
2897             if (!u) {
2898                 x = NULL;
2899                 break;
2900             }
2901             x = PyObject_CallFunctionObjArgs(u, NULL);
2902             Py_DECREF(u);
2903             if (!x)
2904                 break;
2905             /* Setup a finally block (SETUP_WITH as a block is
2906                equivalent to SETUP_FINALLY except it normalizes
2907                the exception) before pushing the result of
2908                __enter__ on the stack. */
2909             PyFrame_BlockSetup(f, SETUP_WITH, INSTR_OFFSET() + oparg,
2910                                STACK_LEVEL());
2911 
2912             PUSH(x);
2913                 DISPATCH();
2914             }
2915         }
2916 
2917         TARGET_NOARG(WITH_CLEANUP)
2918         {
2919             /* At the top of the stack are 1-3 values indicating
2920                how/why we entered the finally clause:
2921                - TOP = None
2922                - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2923                - TOP = WHY_*; no retval below it
2924                - (TOP, SECOND, THIRD) = exc_info()
2925                Below them is EXIT, the context.__exit__ bound method.
2926                In the last case, we must call
2927                  EXIT(TOP, SECOND, THIRD)
2928                otherwise we must call
2929                  EXIT(None, None, None)
2930 
2931                In all cases, we remove EXIT from the stack, leaving
2932                the rest in the same order.
2933 
2934                In addition, if the stack represents an exception,
2935                *and* the function call returns a 'true' value, we
2936                "zap" this information, to prevent END_FINALLY from
2937                re-raising the exception.  (But non-local gotos
2938                should still be resumed.)
2939             */
2940 
2941             PyObject *exit_func;
2942 
2943             u = POP();
2944             if (u == Py_None) {
2945                 exit_func = TOP();
2946                 SET_TOP(u);
2947                 v = w = Py_None;
2948             }
2949             else if (PyInt_Check(u)) {
2950                 switch(PyInt_AS_LONG(u)) {
2951                 case WHY_RETURN:
2952                 case WHY_CONTINUE:
2953                     /* Retval in TOP. */
2954                     exit_func = SECOND();
2955                     SET_SECOND(TOP());
2956                     SET_TOP(u);
2957                     break;
2958                 default:
2959                     exit_func = TOP();
2960                     SET_TOP(u);
2961                     break;
2962                 }
2963                 u = v = w = Py_None;
2964             }
2965             else {
2966                 v = TOP();
2967                 w = SECOND();
2968                 exit_func = THIRD();
2969                 SET_TOP(u);
2970                 SET_SECOND(v);
2971                 SET_THIRD(w);
2972             }
2973             /* XXX Not the fastest way to call it... */
2974             x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2975                                              NULL);
2976             Py_DECREF(exit_func);
2977             if (x == NULL)
2978                 break; /* Go to error exit */
2979 
2980             if (u != Py_None)
2981                 err = PyObject_IsTrue(x);
2982             else
2983                 err = 0;
2984             Py_DECREF(x);
2985 
2986             if (err < 0)
2987                 break; /* Go to error exit */
2988             else if (err > 0) {
2989                 err = 0;
2990                 /* There was an exception and a true return */
2991                 STACKADJ(-2);
2992                 Py_INCREF(Py_None);
2993                 SET_TOP(Py_None);
2994                 Py_DECREF(u);
2995                 Py_DECREF(v);
2996                 Py_DECREF(w);
2997             } else {
2998                 /* The stack was rearranged to remove EXIT
2999                    above. Let END_FINALLY do its thing */
3000             }
3001             PREDICT(END_FINALLY);
3002             break;
3003         }
3004 
3005         TARGET(CALL_FUNCTION)
3006         {
3007             PyObject **sp;
3008             PCALL(PCALL_ALL);
3009             sp = stack_pointer;
3010 #ifdef WITH_TSC
3011             x = call_function(&sp, oparg, &intr0, &intr1);
3012 #else
3013             x = call_function(&sp, oparg);
3014 #endif
3015             stack_pointer = sp;
3016             PUSH(x);
3017             if (x != NULL) DISPATCH();
3018             break;
3019         }
3020 
3021         TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
3022         TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
3023         TARGET(CALL_FUNCTION_VAR_KW)
3024         _call_function_var_kw:
3025         {
3026             int na = oparg & 0xff;
3027             int nk = (oparg>>8) & 0xff;
3028             int flags = (opcode - CALL_FUNCTION) & 3;
3029             int n = na + 2 * nk;
3030             PyObject **pfunc, *func, **sp;
3031             PCALL(PCALL_ALL);
3032             if (flags & CALL_FLAG_VAR)
3033                 n++;
3034             if (flags & CALL_FLAG_KW)
3035                 n++;
3036             pfunc = stack_pointer - n - 1;
3037             func = *pfunc;
3038 
3039             if (PyMethod_Check(func)
3040                 && PyMethod_GET_SELF(func) != NULL) {
3041                 PyObject *self = PyMethod_GET_SELF(func);
3042                 Py_INCREF(self);
3043                 func = PyMethod_GET_FUNCTION(func);
3044                 Py_INCREF(func);
3045                 Py_DECREF(*pfunc);
3046                 *pfunc = self;
3047                 na++;
3048             } else
3049                 Py_INCREF(func);
3050             sp = stack_pointer;
3051             READ_TIMESTAMP(intr0);
3052             x = ext_do_call(func, &sp, flags, na, nk);
3053             READ_TIMESTAMP(intr1);
3054             stack_pointer = sp;
3055             Py_DECREF(func);
3056 
3057             while (stack_pointer > pfunc) {
3058                 w = POP();
3059                 Py_DECREF(w);
3060             }
3061             PUSH(x);
3062             if (x != NULL) DISPATCH();
3063             break;
3064         }
3065 
3066 
3067         TARGET(MAKE_FUNCTION)
3068         {
3069             v = POP(); /* code object */
3070             x = PyFunction_New(v, f->f_globals);
3071             Py_DECREF(v);
3072             /* XXX Maybe this should be a separate opcode? */
3073             if (x != NULL && oparg > 0) {
3074                 v = PyTuple_New(oparg);
3075                 if (v == NULL) {
3076                     Py_DECREF(x);
3077                     x = NULL;
3078                     break;
3079                 }
3080                 while (--oparg >= 0) {
3081                     w = POP();
3082                     PyTuple_SET_ITEM(v, oparg, w);
3083                 }
3084                 err = PyFunction_SetDefaults(x, v);
3085                 Py_DECREF(v);
3086             }
3087             PUSH(x);
3088             break;
3089         }
3090 
3091         TARGET(MAKE_CLOSURE)
3092         {
3093             v = POP(); /* code object */
3094             x = PyFunction_New(v, f->f_globals);
3095             Py_DECREF(v);
3096             if (x != NULL) {
3097                 v = POP();
3098                 if (PyFunction_SetClosure(x, v) != 0) {
3099                     /* Can't happen unless bytecode is corrupt. */
3100                     why = WHY_EXCEPTION;
3101                 }
3102                 Py_DECREF(v);
3103             }
3104             if (x != NULL && oparg > 0) {
3105                 v = PyTuple_New(oparg);
3106                 if (v == NULL) {
3107                     Py_DECREF(x);
3108                     x = NULL;
3109                     break;
3110                 }
3111                 while (--oparg >= 0) {
3112                     w = POP();
3113                     PyTuple_SET_ITEM(v, oparg, w);
3114                 }
3115                 if (PyFunction_SetDefaults(x, v) != 0) {
3116                     /* Can't happen unless
3117                        PyFunction_SetDefaults changes. */
3118                     why = WHY_EXCEPTION;
3119                 }
3120                 Py_DECREF(v);
3121             }
3122             PUSH(x);
3123             break;
3124         }
3125 
3126         TARGET(BUILD_SLICE)
3127         {
3128             if (oparg == 3)
3129                 w = POP();
3130             else
3131                 w = NULL;
3132             v = POP();
3133             u = TOP();
3134             x = PySlice_New(u, v, w);
3135             Py_DECREF(u);
3136             Py_DECREF(v);
3137             Py_XDECREF(w);
3138             SET_TOP(x);
3139             if (x != NULL) DISPATCH();
3140             break;
3141         }
3142 
3143         TARGET(EXTENDED_ARG)
3144         {
3145             opcode = NEXTOP();
3146             oparg = oparg<<16 | NEXTARG();
3147             goto dispatch_opcode;
3148         }
3149 
3150 #if USE_COMPUTED_GOTOS
3151         _unknown_opcode:
3152 #endif
3153         default:
3154             fprintf(stderr,
3155                 "XXX lineno: %d, opcode: %d\n",
3156                 PyFrame_GetLineNumber(f),
3157                 opcode);
3158             PyErr_SetString(PyExc_SystemError, "unknown opcode");
3159             why = WHY_EXCEPTION;
3160             break;
3161 
3162 #ifdef CASE_TOO_BIG
3163         }
3164 #endif
3165 
3166         } /* switch */
3167 
3168         on_error:
3169 
3170         READ_TIMESTAMP(inst1);
3171 
3172         /* Quickly continue if no error occurred */
3173 
3174         if (why == WHY_NOT) {
3175             if (err == 0 && x != NULL) {
3176 #ifdef CHECKEXC
3177                 /* This check is expensive! */
3178                 if (PyErr_Occurred())
3179                     fprintf(stderr,
3180                         "XXX undetected error\n");
3181                 else {
3182 #endif
3183                     READ_TIMESTAMP(loop1);
3184                     continue; /* Normal, fast path */
3185 #ifdef CHECKEXC
3186                 }
3187 #endif
3188             }
3189             why = WHY_EXCEPTION;
3190             x = Py_None;
3191             err = 0;
3192         }
3193 
3194         /* Double-check exception status */
3195 
3196         if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
3197             if (!PyErr_Occurred()) {
3198                 PyErr_SetString(PyExc_SystemError,
3199                     "error return without exception set");
3200                 why = WHY_EXCEPTION;
3201             }
3202         }
3203 #ifdef CHECKEXC
3204         else {
3205             /* This check is expensive! */
3206             if (PyErr_Occurred()) {
3207                 char buf[128];
3208                 sprintf(buf, "Stack unwind with exception "
3209                     "set and why=%d", why);
3210                 Py_FatalError(buf);
3211             }
3212         }
3213 #endif
3214 
3215         /* Log traceback info if this is a real exception */
3216 
3217         if (why == WHY_EXCEPTION) {
3218             PyTraceBack_Here(f);
3219 
3220             if (tstate->c_tracefunc != NULL)
3221                 call_exc_trace(tstate->c_tracefunc,
3222                                tstate->c_traceobj, f);
3223         }
3224 
3225         /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
3226 
3227         if (why == WHY_RERAISE)
3228             why = WHY_EXCEPTION;
3229 
3230         /* Unwind stacks if a (pseudo) exception occurred */
3231 
3232 fast_block_end:
3233         while (why != WHY_NOT && f->f_iblock > 0) {
3234             /* Peek at the current block. */
3235             PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
3236 
3237             assert(why != WHY_YIELD);
3238             if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3239                 why = WHY_NOT;
3240                 JUMPTO(PyInt_AS_LONG(retval));
3241                 Py_DECREF(retval);
3242                 break;
3243             }
3244 
3245             /* Now we have to pop the block. */
3246             f->f_iblock--;
3247 
3248             while (STACK_LEVEL() > b->b_level) {
3249                 v = POP();
3250                 Py_XDECREF(v);
3251             }
3252             if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3253                 why = WHY_NOT;
3254                 JUMPTO(b->b_handler);
3255                 break;
3256             }
3257             if (b->b_type == SETUP_FINALLY ||
3258                 (b->b_type == SETUP_EXCEPT &&
3259                  why == WHY_EXCEPTION) ||
3260                 b->b_type == SETUP_WITH) {
3261                 if (why == WHY_EXCEPTION) {
3262                     PyObject *exc, *val, *tb;
3263                     PyErr_Fetch(&exc, &val, &tb);
3264                     if (val == NULL) {
3265                         val = Py_None;
3266                         Py_INCREF(val);
3267                     }
3268                     /* Make the raw exception data
3269                        available to the handler,
3270                        so a program can emulate the
3271                        Python main loop.  Don't do
3272                        this for 'finally'. */
3273                     if (b->b_type == SETUP_EXCEPT ||
3274                         b->b_type == SETUP_WITH) {
3275                         PyErr_NormalizeException(
3276                             &exc, &val, &tb);
3277                         set_exc_info(tstate,
3278                                      exc, val, tb);
3279                     }
3280                     if (tb == NULL) {
3281                         Py_INCREF(Py_None);
3282                         PUSH(Py_None);
3283                     } else
3284                         PUSH(tb);
3285                     PUSH(val);
3286                     PUSH(exc);
3287                 }
3288                 else {
3289                     if (why & (WHY_RETURN | WHY_CONTINUE))
3290                         PUSH(retval);
3291                     v = PyInt_FromLong((long)why);
3292                     PUSH(v);
3293                 }
3294                 why = WHY_NOT;
3295                 JUMPTO(b->b_handler);
3296                 break;
3297             }
3298         } /* unwind stack */
3299 
3300         /* End the loop if we still have an error (or return) */
3301 
3302         if (why != WHY_NOT)
3303             break;
3304         READ_TIMESTAMP(loop1);
3305 
3306     } /* main loop */
3307 
3308     assert(why != WHY_YIELD);
3309     /* Pop remaining stack entries. */
3310     while (!EMPTY()) {
3311         v = POP();
3312         Py_XDECREF(v);
3313     }
3314 
3315     if (why != WHY_RETURN)
3316         retval = NULL;
3317 
3318 fast_yield:
3319     if (tstate->use_tracing) {
3320         if (tstate->c_tracefunc) {
3321             if (why == WHY_RETURN || why == WHY_YIELD) {
3322                 if (call_trace(tstate->c_tracefunc,
3323                                tstate->c_traceobj, f,
3324                                PyTrace_RETURN, retval)) {
3325                     Py_XDECREF(retval);
3326                     retval = NULL;
3327                     why = WHY_EXCEPTION;
3328                 }
3329             }
3330             else if (why == WHY_EXCEPTION) {
3331                 call_trace_protected(tstate->c_tracefunc,
3332                                      tstate->c_traceobj, f,
3333                                      PyTrace_RETURN, NULL);
3334             }
3335         }
3336         if (tstate->c_profilefunc) {
3337             if (why == WHY_EXCEPTION)
3338                 call_trace_protected(tstate->c_profilefunc,
3339                                      tstate->c_profileobj, f,
3340                                      PyTrace_RETURN, NULL);
3341             else if (call_trace(tstate->c_profilefunc,
3342                                 tstate->c_profileobj, f,
3343                                 PyTrace_RETURN, retval)) {
3344                 Py_XDECREF(retval);
3345                 retval = NULL;
3346                 why = WHY_EXCEPTION;
3347             }
3348         }
3349     }
3350 
3351     if (tstate->frame->f_exc_type != NULL)
3352         reset_exc_info(tstate);
3353     else {
3354         assert(tstate->frame->f_exc_value == NULL);
3355         assert(tstate->frame->f_exc_traceback == NULL);
3356     }
3357 
3358     /* pop frame */
3359 exit_eval_frame:
3360     Py_LeaveRecursiveCall();
3361     tstate->frame = f->f_back;
3362 
3363     return retval;
3364 }
3365 
3366 /* This is gonna seem *real weird*, but if you put some other code between
3367    PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
3368    the test in the if statements in Misc/gdbinit (pystack and pystackv). */
3369 
3370 PyObject *
PyEval_EvalCodeEx(PyCodeObject * co,PyObject * globals,PyObject * locals,PyObject ** args,int argcount,PyObject ** kws,int kwcount,PyObject ** defs,int defcount,PyObject * closure)3371 PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
3372            PyObject **args, int argcount, PyObject **kws, int kwcount,
3373            PyObject **defs, int defcount, PyObject *closure)
3374 {
3375     register PyFrameObject *f;
3376     register PyObject *retval = NULL;
3377     register PyObject **fastlocals, **freevars;
3378     PyThreadState *tstate = PyThreadState_GET();
3379     PyObject *x, *u;
3380 
3381     if (globals == NULL) {
3382         PyErr_SetString(PyExc_SystemError,
3383                         "PyEval_EvalCodeEx: NULL globals");
3384         return NULL;
3385     }
3386 
3387     assert(tstate != NULL);
3388     assert(globals != NULL);
3389     f = PyFrame_New(tstate, co, globals, locals);
3390     if (f == NULL)
3391         return NULL;
3392 
3393     fastlocals = f->f_localsplus;
3394     freevars = f->f_localsplus + co->co_nlocals;
3395 
3396     if (co->co_argcount > 0 ||
3397         co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3398         int i;
3399         int n = argcount;
3400         PyObject *kwdict = NULL;
3401         if (co->co_flags & CO_VARKEYWORDS) {
3402             kwdict = PyDict_New();
3403             if (kwdict == NULL)
3404                 goto fail;
3405             i = co->co_argcount;
3406             if (co->co_flags & CO_VARARGS)
3407                 i++;
3408             SETLOCAL(i, kwdict);
3409         }
3410         if (argcount > co->co_argcount) {
3411             if (!(co->co_flags & CO_VARARGS)) {
3412                 PyErr_Format(PyExc_TypeError,
3413                     "%.200s() takes %s %d "
3414                     "argument%s (%d given)",
3415                     PyString_AsString(co->co_name),
3416                     defcount ? "at most" : "exactly",
3417                     co->co_argcount,
3418                     co->co_argcount == 1 ? "" : "s",
3419                     argcount + kwcount);
3420                 goto fail;
3421             }
3422             n = co->co_argcount;
3423         }
3424         for (i = 0; i < n; i++) {
3425             x = args[i];
3426             Py_INCREF(x);
3427             SETLOCAL(i, x);
3428         }
3429         if (co->co_flags & CO_VARARGS) {
3430             u = PyTuple_New(argcount - n);
3431             if (u == NULL)
3432                 goto fail;
3433             SETLOCAL(co->co_argcount, u);
3434             for (i = n; i < argcount; i++) {
3435                 x = args[i];
3436                 Py_INCREF(x);
3437                 PyTuple_SET_ITEM(u, i-n, x);
3438             }
3439         }
3440         for (i = 0; i < kwcount; i++) {
3441             PyObject **co_varnames;
3442             PyObject *keyword = kws[2*i];
3443             PyObject *value = kws[2*i + 1];
3444             int j;
3445             if (keyword == NULL || !(PyString_Check(keyword)
3446 #ifdef Py_USING_UNICODE
3447                                      || PyUnicode_Check(keyword)
3448 #endif
3449                         )) {
3450                 PyErr_Format(PyExc_TypeError,
3451                     "%.200s() keywords must be strings",
3452                     PyString_AsString(co->co_name));
3453                 goto fail;
3454             }
3455             /* Speed hack: do raw pointer compares. As names are
3456                normally interned this should almost always hit. */
3457             co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3458             for (j = 0; j < co->co_argcount; j++) {
3459                 PyObject *nm = co_varnames[j];
3460                 if (nm == keyword)
3461                     goto kw_found;
3462             }
3463             /* Slow fallback, just in case */
3464             for (j = 0; j < co->co_argcount; j++) {
3465                 PyObject *nm = co_varnames[j];
3466                 int cmp = PyObject_RichCompareBool(
3467                     keyword, nm, Py_EQ);
3468                 if (cmp > 0)
3469                     goto kw_found;
3470                 else if (cmp < 0)
3471                     goto fail;
3472             }
3473             if (kwdict == NULL) {
3474                 PyObject *kwd_str = kwd_as_string(keyword);
3475                 if (kwd_str) {
3476                     PyErr_Format(PyExc_TypeError,
3477                                  "%.200s() got an unexpected "
3478                                  "keyword argument '%.400s'",
3479                                  PyString_AsString(co->co_name),
3480                                  PyString_AsString(kwd_str));
3481                     Py_DECREF(kwd_str);
3482                 }
3483                 goto fail;
3484             }
3485             PyDict_SetItem(kwdict, keyword, value);
3486             continue;
3487           kw_found:
3488             if (GETLOCAL(j) != NULL) {
3489                 PyObject *kwd_str = kwd_as_string(keyword);
3490                 if (kwd_str) {
3491                     PyErr_Format(PyExc_TypeError,
3492                                  "%.200s() got multiple "
3493                                  "values for keyword "
3494                                  "argument '%.400s'",
3495                                  PyString_AsString(co->co_name),
3496                                  PyString_AsString(kwd_str));
3497                     Py_DECREF(kwd_str);
3498                 }
3499                 goto fail;
3500             }
3501             Py_INCREF(value);
3502             SETLOCAL(j, value);
3503         }
3504         if (argcount < co->co_argcount) {
3505             int m = co->co_argcount - defcount;
3506             for (i = argcount; i < m; i++) {
3507                 if (GETLOCAL(i) == NULL) {
3508                     int j, given = 0;
3509                     for (j = 0; j < co->co_argcount; j++)
3510                         if (GETLOCAL(j))
3511                             given++;
3512                     PyErr_Format(PyExc_TypeError,
3513                         "%.200s() takes %s %d "
3514                         "argument%s (%d given)",
3515                         PyString_AsString(co->co_name),
3516                         ((co->co_flags & CO_VARARGS) ||
3517                          defcount) ? "at least"
3518                                    : "exactly",
3519                         m, m == 1 ? "" : "s", given);
3520                     goto fail;
3521                 }
3522             }
3523             if (n > m)
3524                 i = n - m;
3525             else
3526                 i = 0;
3527             for (; i < defcount; i++) {
3528                 if (GETLOCAL(m+i) == NULL) {
3529                     PyObject *def = defs[i];
3530                     Py_INCREF(def);
3531                     SETLOCAL(m+i, def);
3532                 }
3533             }
3534         }
3535     }
3536     else if (argcount > 0 || kwcount > 0) {
3537         PyErr_Format(PyExc_TypeError,
3538                      "%.200s() takes no arguments (%d given)",
3539                      PyString_AsString(co->co_name),
3540                      argcount + kwcount);
3541         goto fail;
3542     }
3543     /* Allocate and initialize storage for cell vars, and copy free
3544        vars into frame.  This isn't too efficient right now. */
3545     if (PyTuple_GET_SIZE(co->co_cellvars)) {
3546         int i, j, nargs, found;
3547         char *cellname, *argname;
3548         PyObject *c;
3549 
3550         nargs = co->co_argcount;
3551         if (co->co_flags & CO_VARARGS)
3552             nargs++;
3553         if (co->co_flags & CO_VARKEYWORDS)
3554             nargs++;
3555 
3556         /* Initialize each cell var, taking into account
3557            cell vars that are initialized from arguments.
3558 
3559            Should arrange for the compiler to put cellvars
3560            that are arguments at the beginning of the cellvars
3561            list so that we can march over it more efficiently?
3562         */
3563         for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3564             cellname = PyString_AS_STRING(
3565                 PyTuple_GET_ITEM(co->co_cellvars, i));
3566             found = 0;
3567             for (j = 0; j < nargs; j++) {
3568                 argname = PyString_AS_STRING(
3569                     PyTuple_GET_ITEM(co->co_varnames, j));
3570                 if (strcmp(cellname, argname) == 0) {
3571                     c = PyCell_New(GETLOCAL(j));
3572                     if (c == NULL)
3573                         goto fail;
3574                     GETLOCAL(co->co_nlocals + i) = c;
3575                     found = 1;
3576                     break;
3577                 }
3578             }
3579             if (found == 0) {
3580                 c = PyCell_New(NULL);
3581                 if (c == NULL)
3582                     goto fail;
3583                 SETLOCAL(co->co_nlocals + i, c);
3584             }
3585         }
3586     }
3587     if (PyTuple_GET_SIZE(co->co_freevars)) {
3588         int i;
3589         for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3590             PyObject *o = PyTuple_GET_ITEM(closure, i);
3591             Py_INCREF(o);
3592             freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3593         }
3594     }
3595 
3596     if (co->co_flags & CO_GENERATOR) {
3597         /* Don't need to keep the reference to f_back, it will be set
3598          * when the generator is resumed. */
3599         Py_CLEAR(f->f_back);
3600 
3601         PCALL(PCALL_GENERATOR);
3602 
3603         /* Create a new generator that owns the ready to run frame
3604          * and return that as the value. */
3605         return PyGen_New(f);
3606     }
3607 
3608     retval = PyEval_EvalFrameEx(f,0);
3609 
3610 fail: /* Jump here from prelude on failure */
3611 
3612     /* decref'ing the frame can cause __del__ methods to get invoked,
3613        which can call back into Python.  While we're done with the
3614        current Python frame (f), the associated C stack is still in use,
3615        so recursion_depth must be boosted for the duration.
3616     */
3617     assert(tstate != NULL);
3618     ++tstate->recursion_depth;
3619     Py_DECREF(f);
3620     --tstate->recursion_depth;
3621     return retval;
3622 }
3623 
3624 
3625 static PyObject *
special_lookup(PyObject * o,char * meth,PyObject ** cache)3626 special_lookup(PyObject *o, char *meth, PyObject **cache)
3627 {
3628     PyObject *res;
3629     if (PyInstance_Check(o)) {
3630         if (!*cache)
3631             return PyObject_GetAttrString(o, meth);
3632         else
3633             return PyObject_GetAttr(o, *cache);
3634     }
3635     res = _PyObject_LookupSpecial(o, meth, cache);
3636     if (res == NULL && !PyErr_Occurred()) {
3637         PyErr_SetObject(PyExc_AttributeError, *cache);
3638         return NULL;
3639     }
3640     return res;
3641 }
3642 
3643 
3644 static PyObject *
kwd_as_string(PyObject * kwd)3645 kwd_as_string(PyObject *kwd) {
3646 #ifdef Py_USING_UNICODE
3647     if (PyString_Check(kwd)) {
3648 #else
3649         assert(PyString_Check(kwd));
3650 #endif
3651         Py_INCREF(kwd);
3652         return kwd;
3653 #ifdef Py_USING_UNICODE
3654     }
3655     return _PyUnicode_AsDefaultEncodedString(kwd, "replace");
3656 #endif
3657 }
3658 
3659 
3660 /* Implementation notes for set_exc_info() and reset_exc_info():
3661 
3662 - Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
3663   'exc_traceback'.  These always travel together.
3664 
3665 - tstate->curexc_ZZZ is the "hot" exception that is set by
3666   PyErr_SetString(), cleared by PyErr_Clear(), and so on.
3667 
3668 - Once an exception is caught by an except clause, it is transferred
3669   from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
3670   can pick it up.  This is the primary task of set_exc_info().
3671   XXX That can't be right:  set_exc_info() doesn't look at tstate->curexc_ZZZ.
3672 
3673 - Now let me explain the complicated dance with frame->f_exc_ZZZ.
3674 
3675   Long ago, when none of this existed, there were just a few globals:
3676   one set corresponding to the "hot" exception, and one set
3677   corresponding to sys.exc_ZZZ.  (Actually, the latter weren't C
3678   globals; they were simply stored as sys.exc_ZZZ.  For backwards
3679   compatibility, they still are!)  The problem was that in code like
3680   this:
3681 
3682      try:
3683     "something that may fail"
3684      except "some exception":
3685     "do something else first"
3686     "print the exception from sys.exc_ZZZ."
3687 
3688   if "do something else first" invoked something that raised and caught
3689   an exception, sys.exc_ZZZ were overwritten.  That was a frequent
3690   cause of subtle bugs.  I fixed this by changing the semantics as
3691   follows:
3692 
3693     - Within one frame, sys.exc_ZZZ will hold the last exception caught
3694       *in that frame*.
3695 
3696     - But initially, and as long as no exception is caught in a given
3697       frame, sys.exc_ZZZ will hold the last exception caught in the
3698       previous frame (or the frame before that, etc.).
3699 
3700   The first bullet fixed the bug in the above example.  The second
3701   bullet was for backwards compatibility: it was (and is) common to
3702   have a function that is called when an exception is caught, and to
3703   have that function access the caught exception via sys.exc_ZZZ.
3704   (Example: traceback.print_exc()).
3705 
3706   At the same time I fixed the problem that sys.exc_ZZZ weren't
3707   thread-safe, by introducing sys.exc_info() which gets it from tstate;
3708   but that's really a separate improvement.
3709 
3710   The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
3711   variables to what they were before the current frame was called.  The
3712   set_exc_info() function saves them on the frame so that
3713   reset_exc_info() can restore them.  The invariant is that
3714   frame->f_exc_ZZZ is NULL iff the current frame never caught an
3715   exception (where "catching" an exception applies only to successful
3716   except clauses); and if the current frame ever caught an exception,
3717   frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
3718   at the start of the current frame.
3719 
3720 */
3721 
3722 static void
set_exc_info(PyThreadState * tstate,PyObject * type,PyObject * value,PyObject * tb)3723 set_exc_info(PyThreadState *tstate,
3724              PyObject *type, PyObject *value, PyObject *tb)
3725 {
3726     PyFrameObject *frame = tstate->frame;
3727     PyObject *tmp_type, *tmp_value, *tmp_tb;
3728 
3729     assert(type != NULL);
3730     assert(frame != NULL);
3731     if (frame->f_exc_type == NULL) {
3732         assert(frame->f_exc_value == NULL);
3733         assert(frame->f_exc_traceback == NULL);
3734         /* This frame didn't catch an exception before. */
3735         /* Save previous exception of this thread in this frame. */
3736         if (tstate->exc_type == NULL) {
3737             /* XXX Why is this set to Py_None? */
3738             Py_INCREF(Py_None);
3739             tstate->exc_type = Py_None;
3740         }
3741         Py_INCREF(tstate->exc_type);
3742         Py_XINCREF(tstate->exc_value);
3743         Py_XINCREF(tstate->exc_traceback);
3744         frame->f_exc_type = tstate->exc_type;
3745         frame->f_exc_value = tstate->exc_value;
3746         frame->f_exc_traceback = tstate->exc_traceback;
3747     }
3748     /* Set new exception for this thread. */
3749     tmp_type = tstate->exc_type;
3750     tmp_value = tstate->exc_value;
3751     tmp_tb = tstate->exc_traceback;
3752     Py_INCREF(type);
3753     Py_XINCREF(value);
3754     Py_XINCREF(tb);
3755     tstate->exc_type = type;
3756     tstate->exc_value = value;
3757     tstate->exc_traceback = tb;
3758     Py_XDECREF(tmp_type);
3759     Py_XDECREF(tmp_value);
3760     Py_XDECREF(tmp_tb);
3761     /* For b/w compatibility */
3762     PySys_SetObject("exc_type", type);
3763     PySys_SetObject("exc_value", value);
3764     PySys_SetObject("exc_traceback", tb);
3765 }
3766 
3767 static void
reset_exc_info(PyThreadState * tstate)3768 reset_exc_info(PyThreadState *tstate)
3769 {
3770     PyFrameObject *frame;
3771     PyObject *tmp_type, *tmp_value, *tmp_tb;
3772 
3773     /* It's a precondition that the thread state's frame caught an
3774      * exception -- verify in a debug build.
3775      */
3776     assert(tstate != NULL);
3777     frame = tstate->frame;
3778     assert(frame != NULL);
3779     assert(frame->f_exc_type != NULL);
3780 
3781     /* Copy the frame's exception info back to the thread state. */
3782     tmp_type = tstate->exc_type;
3783     tmp_value = tstate->exc_value;
3784     tmp_tb = tstate->exc_traceback;
3785     Py_INCREF(frame->f_exc_type);
3786     Py_XINCREF(frame->f_exc_value);
3787     Py_XINCREF(frame->f_exc_traceback);
3788     tstate->exc_type = frame->f_exc_type;
3789     tstate->exc_value = frame->f_exc_value;
3790     tstate->exc_traceback = frame->f_exc_traceback;
3791     Py_XDECREF(tmp_type);
3792     Py_XDECREF(tmp_value);
3793     Py_XDECREF(tmp_tb);
3794 
3795     /* For b/w compatibility */
3796     PySys_SetObject("exc_type", frame->f_exc_type);
3797     PySys_SetObject("exc_value", frame->f_exc_value);
3798     PySys_SetObject("exc_traceback", frame->f_exc_traceback);
3799 
3800     /* Clear the frame's exception info. */
3801     tmp_type = frame->f_exc_type;
3802     tmp_value = frame->f_exc_value;
3803     tmp_tb = frame->f_exc_traceback;
3804     frame->f_exc_type = NULL;
3805     frame->f_exc_value = NULL;
3806     frame->f_exc_traceback = NULL;
3807     Py_DECREF(tmp_type);
3808     Py_XDECREF(tmp_value);
3809     Py_XDECREF(tmp_tb);
3810 }
3811 
3812 /* Logic for the raise statement (too complicated for inlining).
3813    This *consumes* a reference count to each of its arguments. */
3814 static enum why_code
do_raise(PyObject * type,PyObject * value,PyObject * tb)3815 do_raise(PyObject *type, PyObject *value, PyObject *tb)
3816 {
3817     if (type == NULL) {
3818         /* Reraise */
3819         PyThreadState *tstate = PyThreadState_GET();
3820         type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3821         value = tstate->exc_value;
3822         tb = tstate->exc_traceback;
3823         Py_XINCREF(type);
3824         Py_XINCREF(value);
3825         Py_XINCREF(tb);
3826     }
3827 
3828     /* We support the following forms of raise:
3829        raise <class>, <classinstance>
3830        raise <class>, <argument tuple>
3831        raise <class>, None
3832        raise <class>, <argument>
3833        raise <classinstance>, None
3834        raise <string>, <object>
3835        raise <string>, None
3836 
3837        An omitted second argument is the same as None.
3838 
3839        In addition, raise <tuple>, <anything> is the same as
3840        raising the tuple's first item (and it better have one!);
3841        this rule is applied recursively.
3842 
3843        Finally, an optional third argument can be supplied, which
3844        gives the traceback to be substituted (useful when
3845        re-raising an exception after examining it).  */
3846 
3847     /* First, check the traceback argument, replacing None with
3848        NULL. */
3849     if (tb == Py_None) {
3850         Py_DECREF(tb);
3851         tb = NULL;
3852     }
3853     else if (tb != NULL && !PyTraceBack_Check(tb)) {
3854         PyErr_SetString(PyExc_TypeError,
3855                    "raise: arg 3 must be a traceback or None");
3856         goto raise_error;
3857     }
3858 
3859     /* Next, replace a missing value with None */
3860     if (value == NULL) {
3861         value = Py_None;
3862         Py_INCREF(value);
3863     }
3864 
3865     /* Next, repeatedly, replace a tuple exception with its first item */
3866     while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3867         PyObject *tmp = type;
3868         type = PyTuple_GET_ITEM(type, 0);
3869         Py_INCREF(type);
3870         Py_DECREF(tmp);
3871     }
3872 
3873     if (PyExceptionClass_Check(type)) {
3874         PyErr_NormalizeException(&type, &value, &tb);
3875         if (!PyExceptionInstance_Check(value)) {
3876             PyErr_Format(PyExc_TypeError,
3877                          "calling %s() should have returned an instance of "
3878                          "BaseException, not '%s'",
3879                          ((PyTypeObject *)type)->tp_name,
3880                          Py_TYPE(value)->tp_name);
3881             goto raise_error;
3882         }
3883     }
3884     else if (PyExceptionInstance_Check(type)) {
3885         /* Raising an instance.  The value should be a dummy. */
3886         if (value != Py_None) {
3887             PyErr_SetString(PyExc_TypeError,
3888               "instance exception may not have a separate value");
3889             goto raise_error;
3890         }
3891         else {
3892             /* Normalize to raise <class>, <instance> */
3893             Py_DECREF(value);
3894             value = type;
3895             type = PyExceptionInstance_Class(type);
3896             Py_INCREF(type);
3897         }
3898     }
3899     else {
3900         /* Not something you can raise.  You get an exception
3901            anyway, just not what you specified :-) */
3902         PyErr_Format(PyExc_TypeError,
3903                      "exceptions must be old-style classes or "
3904                      "derived from BaseException, not %s",
3905                      type->ob_type->tp_name);
3906         goto raise_error;
3907     }
3908 
3909     assert(PyExceptionClass_Check(type));
3910     if (Py_Py3kWarningFlag && PyClass_Check(type)) {
3911         if (PyErr_WarnEx(PyExc_DeprecationWarning,
3912                         "exceptions must derive from BaseException "
3913                         "in 3.x", 1) < 0)
3914             goto raise_error;
3915     }
3916 
3917     PyErr_Restore(type, value, tb);
3918     if (tb == NULL)
3919         return WHY_EXCEPTION;
3920     else
3921         return WHY_RERAISE;
3922  raise_error:
3923     Py_XDECREF(value);
3924     Py_XDECREF(type);
3925     Py_XDECREF(tb);
3926     return WHY_EXCEPTION;
3927 }
3928 
3929 /* Iterate v argcnt times and store the results on the stack (via decreasing
3930    sp).  Return 1 for success, 0 if error. */
3931 
3932 static int
unpack_iterable(PyObject * v,int argcnt,PyObject ** sp)3933 unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
3934 {
3935     int i = 0;
3936     PyObject *it;  /* iter(v) */
3937     PyObject *w;
3938 
3939     assert(v != NULL);
3940 
3941     it = PyObject_GetIter(v);
3942     if (it == NULL)
3943         goto Error;
3944 
3945     for (; i < argcnt; i++) {
3946         w = PyIter_Next(it);
3947         if (w == NULL) {
3948             /* Iterator done, via error or exhaustion. */
3949             if (!PyErr_Occurred()) {
3950                 PyErr_Format(PyExc_ValueError,
3951                     "need more than %d value%s to unpack",
3952                     i, i == 1 ? "" : "s");
3953             }
3954             goto Error;
3955         }
3956         *--sp = w;
3957     }
3958 
3959     /* We better have exhausted the iterator now. */
3960     w = PyIter_Next(it);
3961     if (w == NULL) {
3962         if (PyErr_Occurred())
3963             goto Error;
3964         Py_DECREF(it);
3965         return 1;
3966     }
3967     Py_DECREF(w);
3968     PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3969     /* fall through */
3970 Error:
3971     for (; i > 0; i--, sp++)
3972         Py_DECREF(*sp);
3973     Py_XDECREF(it);
3974     return 0;
3975 }
3976 
3977 
3978 #ifdef LLTRACE
3979 static int
prtrace(PyObject * v,char * str)3980 prtrace(PyObject *v, char *str)
3981 {
3982     printf("%s ", str);
3983     if (PyObject_Print(v, stdout, 0) != 0)
3984         PyErr_Clear(); /* Don't know what else to do */
3985     printf("\n");
3986     return 1;
3987 }
3988 #endif
3989 
3990 static void
call_exc_trace(Py_tracefunc func,PyObject * self,PyFrameObject * f)3991 call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
3992 {
3993     PyObject *type, *value, *traceback, *arg;
3994     int err;
3995     PyErr_Fetch(&type, &value, &traceback);
3996     if (value == NULL) {
3997         value = Py_None;
3998         Py_INCREF(value);
3999     }
4000     arg = PyTuple_Pack(3, type, value, traceback);
4001     if (arg == NULL) {
4002         PyErr_Restore(type, value, traceback);
4003         return;
4004     }
4005     err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
4006     Py_DECREF(arg);
4007     if (err == 0)
4008         PyErr_Restore(type, value, traceback);
4009     else {
4010         Py_XDECREF(type);
4011         Py_XDECREF(value);
4012         Py_XDECREF(traceback);
4013     }
4014 }
4015 
4016 static int
call_trace_protected(Py_tracefunc func,PyObject * obj,PyFrameObject * frame,int what,PyObject * arg)4017 call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
4018                      int what, PyObject *arg)
4019 {
4020     PyObject *type, *value, *traceback;
4021     int err;
4022     PyErr_Fetch(&type, &value, &traceback);
4023     err = call_trace(func, obj, frame, what, arg);
4024     if (err == 0)
4025     {
4026         PyErr_Restore(type, value, traceback);
4027         return 0;
4028     }
4029     else {
4030         Py_XDECREF(type);
4031         Py_XDECREF(value);
4032         Py_XDECREF(traceback);
4033         return -1;
4034     }
4035 }
4036 
4037 static int
call_trace(Py_tracefunc func,PyObject * obj,PyFrameObject * frame,int what,PyObject * arg)4038 call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
4039            int what, PyObject *arg)
4040 {
4041     register PyThreadState *tstate = frame->f_tstate;
4042     int result;
4043     if (tstate->tracing)
4044         return 0;
4045     tstate->tracing++;
4046     tstate->use_tracing = 0;
4047     result = func(obj, frame, what, arg);
4048     tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4049                            || (tstate->c_profilefunc != NULL));
4050     tstate->tracing--;
4051     return result;
4052 }
4053 
4054 PyObject *
_PyEval_CallTracing(PyObject * func,PyObject * args)4055 _PyEval_CallTracing(PyObject *func, PyObject *args)
4056 {
4057     PyFrameObject *frame = PyEval_GetFrame();
4058     PyThreadState *tstate = frame->f_tstate;
4059     int save_tracing = tstate->tracing;
4060     int save_use_tracing = tstate->use_tracing;
4061     PyObject *result;
4062 
4063     tstate->tracing = 0;
4064     tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4065                            || (tstate->c_profilefunc != NULL));
4066     result = PyObject_Call(func, args, NULL);
4067     tstate->tracing = save_tracing;
4068     tstate->use_tracing = save_use_tracing;
4069     return result;
4070 }
4071 
4072 /* See Objects/lnotab_notes.txt for a description of how tracing works. */
4073 static int
maybe_call_line_trace(Py_tracefunc func,PyObject * obj,PyFrameObject * frame,int * instr_lb,int * instr_ub,int * instr_prev)4074 maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
4075                       PyFrameObject *frame, int *instr_lb, int *instr_ub,
4076                       int *instr_prev)
4077 {
4078     int result = 0;
4079     int line = frame->f_lineno;
4080 
4081     /* If the last instruction executed isn't in the current
4082        instruction window, reset the window.
4083     */
4084     if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4085         PyAddrPair bounds;
4086         line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4087                                        &bounds);
4088         *instr_lb = bounds.ap_lower;
4089         *instr_ub = bounds.ap_upper;
4090     }
4091     /* If the last instruction falls at the start of a line or if
4092        it represents a jump backwards, update the frame's line
4093        number and call the trace function. */
4094     if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4095         frame->f_lineno = line;
4096         result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
4097     }
4098     *instr_prev = frame->f_lasti;
4099     return result;
4100 }
4101 
4102 void
PyEval_SetProfile(Py_tracefunc func,PyObject * arg)4103 PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
4104 {
4105     PyThreadState *tstate = PyThreadState_GET();
4106     PyObject *temp = tstate->c_profileobj;
4107     Py_XINCREF(arg);
4108     tstate->c_profilefunc = NULL;
4109     tstate->c_profileobj = NULL;
4110     /* Must make sure that tracing is not ignored if 'temp' is freed */
4111     tstate->use_tracing = tstate->c_tracefunc != NULL;
4112     Py_XDECREF(temp);
4113     tstate->c_profilefunc = func;
4114     tstate->c_profileobj = arg;
4115     /* Flag that tracing or profiling is turned on */
4116     tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
4117 }
4118 
4119 void
PyEval_SetTrace(Py_tracefunc func,PyObject * arg)4120 PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4121 {
4122     PyThreadState *tstate = PyThreadState_GET();
4123     PyObject *temp = tstate->c_traceobj;
4124     _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4125     Py_XINCREF(arg);
4126     tstate->c_tracefunc = NULL;
4127     tstate->c_traceobj = NULL;
4128     /* Must make sure that profiling is not ignored if 'temp' is freed */
4129     tstate->use_tracing = tstate->c_profilefunc != NULL;
4130     Py_XDECREF(temp);
4131     tstate->c_tracefunc = func;
4132     tstate->c_traceobj = arg;
4133     /* Flag that tracing or profiling is turned on */
4134     tstate->use_tracing = ((func != NULL)
4135                            || (tstate->c_profilefunc != NULL));
4136 }
4137 
4138 PyObject *
PyEval_GetBuiltins(void)4139 PyEval_GetBuiltins(void)
4140 {
4141     PyFrameObject *current_frame = PyEval_GetFrame();
4142     if (current_frame == NULL)
4143         return PyThreadState_GET()->interp->builtins;
4144     else
4145         return current_frame->f_builtins;
4146 }
4147 
4148 PyObject *
PyEval_GetLocals(void)4149 PyEval_GetLocals(void)
4150 {
4151     PyFrameObject *current_frame = PyEval_GetFrame();
4152     if (current_frame == NULL)
4153         return NULL;
4154     PyFrame_FastToLocals(current_frame);
4155     return current_frame->f_locals;
4156 }
4157 
4158 PyObject *
PyEval_GetGlobals(void)4159 PyEval_GetGlobals(void)
4160 {
4161     PyFrameObject *current_frame = PyEval_GetFrame();
4162     if (current_frame == NULL)
4163         return NULL;
4164     else
4165         return current_frame->f_globals;
4166 }
4167 
4168 PyFrameObject *
PyEval_GetFrame(void)4169 PyEval_GetFrame(void)
4170 {
4171     PyThreadState *tstate = PyThreadState_GET();
4172     return _PyThreadState_GetFrame(tstate);
4173 }
4174 
4175 int
PyEval_GetRestricted(void)4176 PyEval_GetRestricted(void)
4177 {
4178     PyFrameObject *current_frame = PyEval_GetFrame();
4179     return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
4180 }
4181 
4182 int
PyEval_MergeCompilerFlags(PyCompilerFlags * cf)4183 PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
4184 {
4185     PyFrameObject *current_frame = PyEval_GetFrame();
4186     int result = cf->cf_flags != 0;
4187 
4188     if (current_frame != NULL) {
4189         const int codeflags = current_frame->f_code->co_flags;
4190         const int compilerflags = codeflags & PyCF_MASK;
4191         if (compilerflags) {
4192             result = 1;
4193             cf->cf_flags |= compilerflags;
4194         }
4195 #if 0 /* future keyword */
4196         if (codeflags & CO_GENERATOR_ALLOWED) {
4197             result = 1;
4198             cf->cf_flags |= CO_GENERATOR_ALLOWED;
4199         }
4200 #endif
4201     }
4202     return result;
4203 }
4204 
4205 int
Py_FlushLine(void)4206 Py_FlushLine(void)
4207 {
4208     PyObject *f = PySys_GetObject("stdout");
4209     if (f == NULL)
4210         return 0;
4211     if (!PyFile_SoftSpace(f, 0))
4212         return 0;
4213     return PyFile_WriteString("\n", f);
4214 }
4215 
4216 
4217 /* External interface to call any callable object.
4218    The arg must be a tuple or NULL.  The kw must be a dict or NULL. */
4219 
4220 PyObject *
PyEval_CallObjectWithKeywords(PyObject * func,PyObject * arg,PyObject * kw)4221 PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
4222 {
4223     PyObject *result;
4224 
4225     if (arg == NULL) {
4226         arg = PyTuple_New(0);
4227         if (arg == NULL)
4228             return NULL;
4229     }
4230     else if (!PyTuple_Check(arg)) {
4231         PyErr_SetString(PyExc_TypeError,
4232                         "argument list must be a tuple");
4233         return NULL;
4234     }
4235     else
4236         Py_INCREF(arg);
4237 
4238     if (kw != NULL && !PyDict_Check(kw)) {
4239         PyErr_SetString(PyExc_TypeError,
4240                         "keyword list must be a dictionary");
4241         Py_DECREF(arg);
4242         return NULL;
4243     }
4244 
4245     result = PyObject_Call(func, arg, kw);
4246     Py_DECREF(arg);
4247     return result;
4248 }
4249 
4250 const char *
PyEval_GetFuncName(PyObject * func)4251 PyEval_GetFuncName(PyObject *func)
4252 {
4253     if (PyMethod_Check(func))
4254         return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4255     else if (PyFunction_Check(func))
4256         return PyString_AsString(((PyFunctionObject*)func)->func_name);
4257     else if (PyCFunction_Check(func))
4258         return ((PyCFunctionObject*)func)->m_ml->ml_name;
4259     else if (PyClass_Check(func))
4260         return PyString_AsString(((PyClassObject*)func)->cl_name);
4261     else if (PyInstance_Check(func)) {
4262         return PyString_AsString(
4263             ((PyInstanceObject*)func)->in_class->cl_name);
4264     } else {
4265         return func->ob_type->tp_name;
4266     }
4267 }
4268 
4269 const char *
PyEval_GetFuncDesc(PyObject * func)4270 PyEval_GetFuncDesc(PyObject *func)
4271 {
4272     if (PyMethod_Check(func))
4273         return "()";
4274     else if (PyFunction_Check(func))
4275         return "()";
4276     else if (PyCFunction_Check(func))
4277         return "()";
4278     else if (PyClass_Check(func))
4279         return " constructor";
4280     else if (PyInstance_Check(func)) {
4281         return " instance";
4282     } else {
4283         return " object";
4284     }
4285 }
4286 
4287 static void
err_args(PyObject * func,int flags,int nargs)4288 err_args(PyObject *func, int flags, int nargs)
4289 {
4290     if (flags & METH_NOARGS)
4291         PyErr_Format(PyExc_TypeError,
4292                      "%.200s() takes no arguments (%d given)",
4293                      ((PyCFunctionObject *)func)->m_ml->ml_name,
4294                      nargs);
4295     else
4296         PyErr_Format(PyExc_TypeError,
4297                      "%.200s() takes exactly one argument (%d given)",
4298                      ((PyCFunctionObject *)func)->m_ml->ml_name,
4299                      nargs);
4300 }
4301 
4302 #define C_TRACE(x, call) \
4303 if (tstate->use_tracing && tstate->c_profilefunc) { \
4304     if (call_trace(tstate->c_profilefunc, \
4305         tstate->c_profileobj, \
4306         tstate->frame, PyTrace_C_CALL, \
4307         func)) { \
4308         x = NULL; \
4309     } \
4310     else { \
4311         x = call; \
4312         if (tstate->c_profilefunc != NULL) { \
4313             if (x == NULL) { \
4314                 call_trace_protected(tstate->c_profilefunc, \
4315                     tstate->c_profileobj, \
4316                     tstate->frame, PyTrace_C_EXCEPTION, \
4317                     func); \
4318                 /* XXX should pass (type, value, tb) */ \
4319             } else { \
4320                 if (call_trace(tstate->c_profilefunc, \
4321                     tstate->c_profileobj, \
4322                     tstate->frame, PyTrace_C_RETURN, \
4323                     func)) { \
4324                     Py_DECREF(x); \
4325                     x = NULL; \
4326                 } \
4327             } \
4328         } \
4329     } \
4330 } else { \
4331     x = call; \
4332     }
4333 
4334 static PyObject *
call_function(PyObject *** pp_stack,int oparg,uint64 * pintr0,uint64 * pintr1)4335 call_function(PyObject ***pp_stack, int oparg
4336 #ifdef WITH_TSC
4337                 , uint64* pintr0, uint64* pintr1
4338 #endif
4339                 )
4340 {
4341     int na = oparg & 0xff;
4342     int nk = (oparg>>8) & 0xff;
4343     int n = na + 2 * nk;
4344     PyObject **pfunc = (*pp_stack) - n - 1;
4345     PyObject *func = *pfunc;
4346     PyObject *x, *w;
4347 
4348     /* Always dispatch PyCFunction first, because these are
4349        presumed to be the most frequent callable object.
4350     */
4351     if (PyCFunction_Check(func) && nk == 0) {
4352         int flags = PyCFunction_GET_FLAGS(func);
4353         PyThreadState *tstate = PyThreadState_GET();
4354 
4355         PCALL(PCALL_CFUNCTION);
4356         if (flags & (METH_NOARGS | METH_O)) {
4357             PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4358             PyObject *self = PyCFunction_GET_SELF(func);
4359             if (flags & METH_NOARGS && na == 0) {
4360                 C_TRACE(x, (*meth)(self,NULL));
4361             }
4362             else if (flags & METH_O && na == 1) {
4363                 PyObject *arg = EXT_POP(*pp_stack);
4364                 C_TRACE(x, (*meth)(self,arg));
4365                 Py_DECREF(arg);
4366             }
4367             else {
4368                 err_args(func, flags, na);
4369                 x = NULL;
4370             }
4371         }
4372         else {
4373             PyObject *callargs;
4374             callargs = load_args(pp_stack, na);
4375             READ_TIMESTAMP(*pintr0);
4376             C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4377             READ_TIMESTAMP(*pintr1);
4378             Py_XDECREF(callargs);
4379         }
4380     } else {
4381         if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4382             /* optimize access to bound methods */
4383             PyObject *self = PyMethod_GET_SELF(func);
4384             PCALL(PCALL_METHOD);
4385             PCALL(PCALL_BOUND_METHOD);
4386             Py_INCREF(self);
4387             func = PyMethod_GET_FUNCTION(func);
4388             Py_INCREF(func);
4389             Py_SETREF(*pfunc, self);
4390             na++;
4391             n++;
4392         } else
4393             Py_INCREF(func);
4394         READ_TIMESTAMP(*pintr0);
4395         if (PyFunction_Check(func))
4396             x = fast_function(func, pp_stack, n, na, nk);
4397         else
4398             x = do_call(func, pp_stack, na, nk);
4399         READ_TIMESTAMP(*pintr1);
4400         Py_DECREF(func);
4401     }
4402 
4403     /* Clear the stack of the function object.  Also removes
4404        the arguments in case they weren't consumed already
4405        (fast_function() and err_args() leave them on the stack).
4406      */
4407     while ((*pp_stack) > pfunc) {
4408         w = EXT_POP(*pp_stack);
4409         Py_DECREF(w);
4410         PCALL(PCALL_POP);
4411     }
4412     return x;
4413 }
4414 
4415 /* The fast_function() function optimize calls for which no argument
4416    tuple is necessary; the objects are passed directly from the stack.
4417    For the simplest case -- a function that takes only positional
4418    arguments and is called with only positional arguments -- it
4419    inlines the most primitive frame setup code from
4420    PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4421    done before evaluating the frame.
4422 */
4423 
4424 static PyObject *
fast_function(PyObject * func,PyObject *** pp_stack,int n,int na,int nk)4425 fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
4426 {
4427     PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4428     PyObject *globals = PyFunction_GET_GLOBALS(func);
4429     PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4430     PyObject **d = NULL;
4431     int nd = 0;
4432 
4433     PCALL(PCALL_FUNCTION);
4434     PCALL(PCALL_FAST_FUNCTION);
4435     if (argdefs == NULL && co->co_argcount == n && nk==0 &&
4436         co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4437         PyFrameObject *f;
4438         PyObject *retval = NULL;
4439         PyThreadState *tstate = PyThreadState_GET();
4440         PyObject **fastlocals, **stack;
4441         int i;
4442 
4443         PCALL(PCALL_FASTER_FUNCTION);
4444         assert(globals != NULL);
4445         /* XXX Perhaps we should create a specialized
4446            PyFrame_New() that doesn't take locals, but does
4447            take builtins without sanity checking them.
4448         */
4449         assert(tstate != NULL);
4450         f = PyFrame_New(tstate, co, globals, NULL);
4451         if (f == NULL)
4452             return NULL;
4453 
4454         fastlocals = f->f_localsplus;
4455         stack = (*pp_stack) - n;
4456 
4457         for (i = 0; i < n; i++) {
4458             Py_INCREF(*stack);
4459             fastlocals[i] = *stack++;
4460         }
4461         retval = PyEval_EvalFrameEx(f,0);
4462         ++tstate->recursion_depth;
4463         Py_DECREF(f);
4464         --tstate->recursion_depth;
4465         return retval;
4466     }
4467     if (argdefs != NULL) {
4468         d = &PyTuple_GET_ITEM(argdefs, 0);
4469         nd = Py_SIZE(argdefs);
4470     }
4471     return PyEval_EvalCodeEx(co, globals,
4472                              (PyObject *)NULL, (*pp_stack)-n, na,
4473                              (*pp_stack)-2*nk, nk, d, nd,
4474                              PyFunction_GET_CLOSURE(func));
4475 }
4476 
4477 static PyObject *
update_keyword_args(PyObject * orig_kwdict,int nk,PyObject *** pp_stack,PyObject * func)4478 update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4479                     PyObject *func)
4480 {
4481     PyObject *kwdict = NULL;
4482     if (orig_kwdict == NULL)
4483         kwdict = PyDict_New();
4484     else {
4485         kwdict = PyDict_Copy(orig_kwdict);
4486         Py_DECREF(orig_kwdict);
4487     }
4488     if (kwdict == NULL)
4489         return NULL;
4490     while (--nk >= 0) {
4491         int err;
4492         PyObject *value = EXT_POP(*pp_stack);
4493         PyObject *key = EXT_POP(*pp_stack);
4494         if (PyDict_GetItem(kwdict, key) != NULL) {
4495             PyErr_Format(PyExc_TypeError,
4496                          "%.200s%s got multiple values "
4497                          "for keyword argument '%.200s'",
4498                          PyEval_GetFuncName(func),
4499                          PyEval_GetFuncDesc(func),
4500                          PyString_AsString(key));
4501             Py_DECREF(key);
4502             Py_DECREF(value);
4503             Py_DECREF(kwdict);
4504             return NULL;
4505         }
4506         err = PyDict_SetItem(kwdict, key, value);
4507         Py_DECREF(key);
4508         Py_DECREF(value);
4509         if (err) {
4510             Py_DECREF(kwdict);
4511             return NULL;
4512         }
4513     }
4514     return kwdict;
4515 }
4516 
4517 static PyObject *
update_star_args(int nstack,int nstar,PyObject * stararg,PyObject *** pp_stack)4518 update_star_args(int nstack, int nstar, PyObject *stararg,
4519                  PyObject ***pp_stack)
4520 {
4521     PyObject *callargs, *w;
4522 
4523     callargs = PyTuple_New(nstack + nstar);
4524     if (callargs == NULL) {
4525         return NULL;
4526     }
4527     if (nstar) {
4528         int i;
4529         for (i = 0; i < nstar; i++) {
4530             PyObject *a = PyTuple_GET_ITEM(stararg, i);
4531             Py_INCREF(a);
4532             PyTuple_SET_ITEM(callargs, nstack + i, a);
4533         }
4534     }
4535     while (--nstack >= 0) {
4536         w = EXT_POP(*pp_stack);
4537         PyTuple_SET_ITEM(callargs, nstack, w);
4538     }
4539     return callargs;
4540 }
4541 
4542 static PyObject *
load_args(PyObject *** pp_stack,int na)4543 load_args(PyObject ***pp_stack, int na)
4544 {
4545     PyObject *args = PyTuple_New(na);
4546     PyObject *w;
4547 
4548     if (args == NULL)
4549         return NULL;
4550     while (--na >= 0) {
4551         w = EXT_POP(*pp_stack);
4552         PyTuple_SET_ITEM(args, na, w);
4553     }
4554     return args;
4555 }
4556 
4557 static PyObject *
do_call(PyObject * func,PyObject *** pp_stack,int na,int nk)4558 do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4559 {
4560     PyObject *callargs = NULL;
4561     PyObject *kwdict = NULL;
4562     PyObject *result = NULL;
4563 
4564     if (nk > 0) {
4565         kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4566         if (kwdict == NULL)
4567             goto call_fail;
4568     }
4569     callargs = load_args(pp_stack, na);
4570     if (callargs == NULL)
4571         goto call_fail;
4572 #ifdef CALL_PROFILE
4573     /* At this point, we have to look at the type of func to
4574        update the call stats properly.  Do it here so as to avoid
4575        exposing the call stats machinery outside ceval.c
4576     */
4577     if (PyFunction_Check(func))
4578         PCALL(PCALL_FUNCTION);
4579     else if (PyMethod_Check(func))
4580         PCALL(PCALL_METHOD);
4581     else if (PyType_Check(func))
4582         PCALL(PCALL_TYPE);
4583     else if (PyCFunction_Check(func))
4584         PCALL(PCALL_CFUNCTION);
4585     else
4586         PCALL(PCALL_OTHER);
4587 #endif
4588     if (PyCFunction_Check(func)) {
4589         PyThreadState *tstate = PyThreadState_GET();
4590         C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4591     }
4592     else
4593         result = PyObject_Call(func, callargs, kwdict);
4594  call_fail:
4595     Py_XDECREF(callargs);
4596     Py_XDECREF(kwdict);
4597     return result;
4598 }
4599 
4600 static PyObject *
ext_do_call(PyObject * func,PyObject *** pp_stack,int flags,int na,int nk)4601 ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4602 {
4603     int nstar = 0;
4604     PyObject *callargs = NULL;
4605     PyObject *stararg = NULL;
4606     PyObject *kwdict = NULL;
4607     PyObject *result = NULL;
4608 
4609     if (flags & CALL_FLAG_KW) {
4610         kwdict = EXT_POP(*pp_stack);
4611         if (!PyDict_Check(kwdict)) {
4612             PyObject *d;
4613             d = PyDict_New();
4614             if (d == NULL)
4615                 goto ext_call_fail;
4616             if (PyDict_Update(d, kwdict) != 0) {
4617                 Py_DECREF(d);
4618                 /* PyDict_Update raises attribute
4619                  * error (percolated from an attempt
4620                  * to get 'keys' attribute) instead of
4621                  * a type error if its second argument
4622                  * is not a mapping.
4623                  */
4624                 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4625                     PyErr_Format(PyExc_TypeError,
4626                                  "%.200s%.200s argument after ** "
4627                                  "must be a mapping, not %.200s",
4628                                  PyEval_GetFuncName(func),
4629                                  PyEval_GetFuncDesc(func),
4630                                  kwdict->ob_type->tp_name);
4631                 }
4632                 goto ext_call_fail;
4633             }
4634             Py_DECREF(kwdict);
4635             kwdict = d;
4636         }
4637     }
4638     if (flags & CALL_FLAG_VAR) {
4639         stararg = EXT_POP(*pp_stack);
4640         if (!PyTuple_Check(stararg)) {
4641             PyObject *t = NULL;
4642             t = PySequence_Tuple(stararg);
4643             if (t == NULL) {
4644                 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4645                         /* Don't mask TypeError raised from a generator */
4646                         !PyGen_Check(stararg)) {
4647                     PyErr_Format(PyExc_TypeError,
4648                                  "%.200s%.200s argument after * "
4649                                  "must be an iterable, not %200s",
4650                                  PyEval_GetFuncName(func),
4651                                  PyEval_GetFuncDesc(func),
4652                                  stararg->ob_type->tp_name);
4653                 }
4654                 goto ext_call_fail;
4655             }
4656             Py_DECREF(stararg);
4657             stararg = t;
4658         }
4659         nstar = PyTuple_GET_SIZE(stararg);
4660     }
4661     if (nk > 0) {
4662         kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4663         if (kwdict == NULL)
4664             goto ext_call_fail;
4665     }
4666     callargs = update_star_args(na, nstar, stararg, pp_stack);
4667     if (callargs == NULL)
4668         goto ext_call_fail;
4669 #ifdef CALL_PROFILE
4670     /* At this point, we have to look at the type of func to
4671        update the call stats properly.  Do it here so as to avoid
4672        exposing the call stats machinery outside ceval.c
4673     */
4674     if (PyFunction_Check(func))
4675         PCALL(PCALL_FUNCTION);
4676     else if (PyMethod_Check(func))
4677         PCALL(PCALL_METHOD);
4678     else if (PyType_Check(func))
4679         PCALL(PCALL_TYPE);
4680     else if (PyCFunction_Check(func))
4681         PCALL(PCALL_CFUNCTION);
4682     else
4683         PCALL(PCALL_OTHER);
4684 #endif
4685     if (PyCFunction_Check(func)) {
4686         PyThreadState *tstate = PyThreadState_GET();
4687         C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4688     }
4689     else
4690         result = PyObject_Call(func, callargs, kwdict);
4691 ext_call_fail:
4692     Py_XDECREF(callargs);
4693     Py_XDECREF(kwdict);
4694     Py_XDECREF(stararg);
4695     return result;
4696 }
4697 
4698 /* Extract a slice index from a PyInt or PyLong or an object with the
4699    nb_index slot defined, and store in *pi.
4700    Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4701    and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
4702    Return 0 on error, 1 on success.
4703 */
4704 /* Note:  If v is NULL, return success without storing into *pi.  This
4705    is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4706    called by the SLICE opcode with v and/or w equal to NULL.
4707 */
4708 int
_PyEval_SliceIndex(PyObject * v,Py_ssize_t * pi)4709 _PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
4710 {
4711     if (v != NULL && v != Py_None) {
4712         Py_ssize_t x;
4713         if (PyInt_Check(v)) {
4714             /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
4715                however, it looks like it should be AsSsize_t.
4716                There should be a comment here explaining why.
4717             */
4718             x = PyInt_AS_LONG(v);
4719         }
4720         else if (PyIndex_Check(v)) {
4721             x = PyNumber_AsSsize_t(v, NULL);
4722             if (x == -1 && PyErr_Occurred())
4723                 return 0;
4724         }
4725         else {
4726             PyErr_SetString(PyExc_TypeError,
4727                             "slice indices must be integers or "
4728                             "None or have an __index__ method");
4729             return 0;
4730         }
4731         *pi = x;
4732     }
4733     return 1;
4734 }
4735 
4736 int
_PyEval_SliceIndexNotNone(PyObject * v,Py_ssize_t * pi)4737 _PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
4738 {
4739     Py_ssize_t x;
4740     if (PyIndex_Check(v)) {
4741         x = PyNumber_AsSsize_t(v, NULL);
4742         if (x == -1 && PyErr_Occurred())
4743             return 0;
4744     }
4745     else {
4746         PyErr_SetString(PyExc_TypeError,
4747                         "slice indices must be integers or "
4748                         "have an __index__ method");
4749         return 0;
4750     }
4751     *pi = x;
4752     return 1;
4753 }
4754 
4755 
4756 #undef ISINDEX
4757 #define ISINDEX(x) ((x) == NULL || _PyAnyInt_Check(x) || PyIndex_Check(x))
4758 
4759 static PyObject *
apply_slice(PyObject * u,PyObject * v,PyObject * w)4760 apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
4761 {
4762     PyTypeObject *tp = u->ob_type;
4763     PySequenceMethods *sq = tp->tp_as_sequence;
4764 
4765     if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
4766         Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4767         if (!_PyEval_SliceIndex(v, &ilow))
4768             return NULL;
4769         if (!_PyEval_SliceIndex(w, &ihigh))
4770             return NULL;
4771         return PySequence_GetSlice(u, ilow, ihigh);
4772     }
4773     else {
4774         PyObject *slice = PySlice_New(v, w, NULL);
4775         if (slice != NULL) {
4776             PyObject *res = PyObject_GetItem(u, slice);
4777             Py_DECREF(slice);
4778             return res;
4779         }
4780         else
4781             return NULL;
4782     }
4783 }
4784 
4785 static int
assign_slice(PyObject * u,PyObject * v,PyObject * w,PyObject * x)4786 assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
4787     /* u[v:w] = x */
4788 {
4789     PyTypeObject *tp = u->ob_type;
4790     PySequenceMethods *sq = tp->tp_as_sequence;
4791 
4792     if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
4793         Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4794         if (!_PyEval_SliceIndex(v, &ilow))
4795             return -1;
4796         if (!_PyEval_SliceIndex(w, &ihigh))
4797             return -1;
4798         if (x == NULL)
4799             return PySequence_DelSlice(u, ilow, ihigh);
4800         else
4801             return PySequence_SetSlice(u, ilow, ihigh, x);
4802     }
4803     else {
4804         PyObject *slice = PySlice_New(v, w, NULL);
4805         if (slice != NULL) {
4806             int res;
4807             if (x != NULL)
4808                 res = PyObject_SetItem(u, slice, x);
4809             else
4810                 res = PyObject_DelItem(u, slice);
4811             Py_DECREF(slice);
4812             return res;
4813         }
4814         else
4815             return -1;
4816     }
4817 }
4818 
4819 #define Py3kExceptionClass_Check(x)     \
4820     (PyType_Check((x)) &&               \
4821      PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4822 
4823 #define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
4824                          "BaseException is not allowed in 3.x"
4825 
4826 static PyObject *
cmp_outcome(int op,register PyObject * v,register PyObject * w)4827 cmp_outcome(int op, register PyObject *v, register PyObject *w)
4828 {
4829     int res = 0;
4830     switch (op) {
4831     case PyCmp_IS:
4832         res = (v == w);
4833         break;
4834     case PyCmp_IS_NOT:
4835         res = (v != w);
4836         break;
4837     case PyCmp_IN:
4838         res = PySequence_Contains(w, v);
4839         if (res < 0)
4840             return NULL;
4841         break;
4842     case PyCmp_NOT_IN:
4843         res = PySequence_Contains(w, v);
4844         if (res < 0)
4845             return NULL;
4846         res = !res;
4847         break;
4848     case PyCmp_EXC_MATCH:
4849         if (PyTuple_Check(w)) {
4850             Py_ssize_t i, length;
4851             length = PyTuple_Size(w);
4852             for (i = 0; i < length; i += 1) {
4853                 PyObject *exc = PyTuple_GET_ITEM(w, i);
4854                 if (PyString_Check(exc)) {
4855                     int ret_val;
4856                     ret_val = PyErr_WarnEx(
4857                         PyExc_DeprecationWarning,
4858                         "catching of string "
4859                         "exceptions is deprecated", 1);
4860                     if (ret_val < 0)
4861                         return NULL;
4862                 }
4863                 else if (Py_Py3kWarningFlag  &&
4864                          !PyTuple_Check(exc) &&
4865                          !Py3kExceptionClass_Check(exc))
4866                 {
4867                     int ret_val;
4868                     ret_val = PyErr_WarnEx(
4869                         PyExc_DeprecationWarning,
4870                         CANNOT_CATCH_MSG, 1);
4871                     if (ret_val < 0)
4872                         return NULL;
4873                 }
4874             }
4875         }
4876         else {
4877             if (PyString_Check(w)) {
4878                 int ret_val;
4879                 ret_val = PyErr_WarnEx(
4880                                 PyExc_DeprecationWarning,
4881                                 "catching of string "
4882                                 "exceptions is deprecated", 1);
4883                 if (ret_val < 0)
4884                     return NULL;
4885             }
4886             else if (Py_Py3kWarningFlag  &&
4887                      !PyTuple_Check(w) &&
4888                      !Py3kExceptionClass_Check(w))
4889             {
4890                 int ret_val;
4891                 ret_val = PyErr_WarnEx(
4892                     PyExc_DeprecationWarning,
4893                     CANNOT_CATCH_MSG, 1);
4894                 if (ret_val < 0)
4895                     return NULL;
4896             }
4897         }
4898         res = PyErr_GivenExceptionMatches(v, w);
4899         break;
4900     default:
4901         return PyObject_RichCompare(v, w, op);
4902     }
4903     v = res ? Py_True : Py_False;
4904     Py_INCREF(v);
4905     return v;
4906 }
4907 
4908 static PyObject *
import_from(PyObject * v,PyObject * name)4909 import_from(PyObject *v, PyObject *name)
4910 {
4911     PyObject *x;
4912 
4913     x = PyObject_GetAttr(v, name);
4914     if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4915         PyErr_Format(PyExc_ImportError,
4916                      "cannot import name %.230s",
4917                      PyString_AsString(name));
4918     }
4919     return x;
4920 }
4921 
4922 static int
import_all_from(PyObject * locals,PyObject * v)4923 import_all_from(PyObject *locals, PyObject *v)
4924 {
4925     PyObject *all = PyObject_GetAttrString(v, "__all__");
4926     PyObject *dict, *name, *value;
4927     int skip_leading_underscores = 0;
4928     int pos, err;
4929 
4930     if (all == NULL) {
4931         if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4932             return -1; /* Unexpected error */
4933         PyErr_Clear();
4934         dict = PyObject_GetAttrString(v, "__dict__");
4935         if (dict == NULL) {
4936             if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4937                 return -1;
4938             PyErr_SetString(PyExc_ImportError,
4939             "from-import-* object has no __dict__ and no __all__");
4940             return -1;
4941         }
4942         all = PyMapping_Keys(dict);
4943         Py_DECREF(dict);
4944         if (all == NULL)
4945             return -1;
4946         skip_leading_underscores = 1;
4947     }
4948 
4949     for (pos = 0, err = 0; ; pos++) {
4950         name = PySequence_GetItem(all, pos);
4951         if (name == NULL) {
4952             if (!PyErr_ExceptionMatches(PyExc_IndexError))
4953                 err = -1;
4954             else
4955                 PyErr_Clear();
4956             break;
4957         }
4958         if (skip_leading_underscores &&
4959             PyString_Check(name) &&
4960             PyString_AS_STRING(name)[0] == '_')
4961         {
4962             Py_DECREF(name);
4963             continue;
4964         }
4965         value = PyObject_GetAttr(v, name);
4966         if (value == NULL)
4967             err = -1;
4968         else if (PyDict_CheckExact(locals))
4969             err = PyDict_SetItem(locals, name, value);
4970         else
4971             err = PyObject_SetItem(locals, name, value);
4972         Py_DECREF(name);
4973         Py_XDECREF(value);
4974         if (err != 0)
4975             break;
4976     }
4977     Py_DECREF(all);
4978     return err;
4979 }
4980 
4981 static PyObject *
build_class(PyObject * methods,PyObject * bases,PyObject * name)4982 build_class(PyObject *methods, PyObject *bases, PyObject *name)
4983 {
4984     PyObject *metaclass = NULL, *result, *base;
4985 
4986     if (PyDict_Check(methods))
4987         metaclass = PyDict_GetItemString(methods, "__metaclass__");
4988     if (metaclass != NULL)
4989         Py_INCREF(metaclass);
4990     else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4991         base = PyTuple_GET_ITEM(bases, 0);
4992         metaclass = PyObject_GetAttrString(base, "__class__");
4993         if (metaclass == NULL) {
4994             PyErr_Clear();
4995             metaclass = (PyObject *)base->ob_type;
4996             Py_INCREF(metaclass);
4997         }
4998     }
4999     else {
5000         PyObject *g = PyEval_GetGlobals();
5001         if (g != NULL && PyDict_Check(g))
5002             metaclass = PyDict_GetItemString(g, "__metaclass__");
5003         if (metaclass == NULL)
5004             metaclass = (PyObject *) &PyClass_Type;
5005         Py_INCREF(metaclass);
5006     }
5007     result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods,
5008                                           NULL);
5009     Py_DECREF(metaclass);
5010     if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
5011         /* A type error here likely means that the user passed
5012            in a base that was not a class (such the random module
5013            instead of the random.random type).  Help them out with
5014            by augmenting the error message with more information.*/
5015 
5016         PyObject *ptype, *pvalue, *ptraceback;
5017 
5018         PyErr_Fetch(&ptype, &pvalue, &ptraceback);
5019         if (PyString_Check(pvalue)) {
5020             PyObject *newmsg;
5021             newmsg = PyString_FromFormat(
5022                 "Error when calling the metaclass bases\n"
5023                 "    %s",
5024                 PyString_AS_STRING(pvalue));
5025             if (newmsg != NULL) {
5026                 Py_DECREF(pvalue);
5027                 pvalue = newmsg;
5028             }
5029         }
5030         PyErr_Restore(ptype, pvalue, ptraceback);
5031     }
5032     return result;
5033 }
5034 
5035 static int
exec_statement(PyFrameObject * f,PyObject * prog,PyObject * globals,PyObject * locals)5036 exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
5037                PyObject *locals)
5038 {
5039     int n;
5040     PyObject *v;
5041     int plain = 0;
5042 
5043     if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
5044         ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
5045         /* Backward compatibility hack */
5046         globals = PyTuple_GetItem(prog, 1);
5047         if (n == 3)
5048             locals = PyTuple_GetItem(prog, 2);
5049         prog = PyTuple_GetItem(prog, 0);
5050     }
5051     if (globals == Py_None) {
5052         globals = PyEval_GetGlobals();
5053         if (locals == Py_None) {
5054             locals = PyEval_GetLocals();
5055             plain = 1;
5056         }
5057         if (!globals || !locals) {
5058             PyErr_SetString(PyExc_SystemError,
5059                             "globals and locals cannot be NULL");
5060             return -1;
5061         }
5062     }
5063     else if (locals == Py_None)
5064         locals = globals;
5065     if (!PyString_Check(prog) &&
5066 #ifdef Py_USING_UNICODE
5067         !PyUnicode_Check(prog) &&
5068 #endif
5069         !PyCode_Check(prog) &&
5070         !PyFile_Check(prog)) {
5071         PyErr_SetString(PyExc_TypeError,
5072             "exec: arg 1 must be a string, file, or code object");
5073         return -1;
5074     }
5075     if (!PyDict_Check(globals)) {
5076         PyErr_SetString(PyExc_TypeError,
5077             "exec: arg 2 must be a dictionary or None");
5078         return -1;
5079     }
5080     if (!PyMapping_Check(locals)) {
5081         PyErr_SetString(PyExc_TypeError,
5082             "exec: arg 3 must be a mapping or None");
5083         return -1;
5084     }
5085     if (PyDict_GetItemString(globals, "__builtins__") == NULL)
5086         PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
5087     if (PyCode_Check(prog)) {
5088         if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
5089             PyErr_SetString(PyExc_TypeError,
5090         "code object passed to exec may not contain free variables");
5091             return -1;
5092         }
5093         v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
5094     }
5095     else if (PyFile_Check(prog)) {
5096         FILE *fp = PyFile_AsFile(prog);
5097         char *name = PyString_AsString(PyFile_Name(prog));
5098         PyCompilerFlags cf;
5099         if (name == NULL)
5100             return -1;
5101         cf.cf_flags = 0;
5102         if (PyEval_MergeCompilerFlags(&cf))
5103             v = PyRun_FileFlags(fp, name, Py_file_input, globals,
5104                                 locals, &cf);
5105         else
5106             v = PyRun_File(fp, name, Py_file_input, globals,
5107                            locals);
5108     }
5109     else {
5110         PyObject *tmp = NULL;
5111         char *str;
5112         PyCompilerFlags cf;
5113         cf.cf_flags = 0;
5114 #ifdef Py_USING_UNICODE
5115         if (PyUnicode_Check(prog)) {
5116             tmp = PyUnicode_AsUTF8String(prog);
5117             if (tmp == NULL)
5118                 return -1;
5119             prog = tmp;
5120             cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
5121         }
5122 #endif
5123         if (PyString_AsStringAndSize(prog, &str, NULL))
5124             return -1;
5125         if (PyEval_MergeCompilerFlags(&cf))
5126             v = PyRun_StringFlags(str, Py_file_input, globals,
5127                                   locals, &cf);
5128         else
5129             v = PyRun_String(str, Py_file_input, globals, locals);
5130         Py_XDECREF(tmp);
5131     }
5132     if (plain)
5133         PyFrame_LocalsToFast(f, 0);
5134     if (v == NULL)
5135         return -1;
5136     Py_DECREF(v);
5137     return 0;
5138 }
5139 
5140 static void
format_exc_check_arg(PyObject * exc,char * format_str,PyObject * obj)5141 format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
5142 {
5143     char *obj_str;
5144 
5145     if (!obj)
5146         return;
5147 
5148     obj_str = PyString_AsString(obj);
5149     if (!obj_str)
5150         return;
5151 
5152     PyErr_Format(exc, format_str, obj_str);
5153 }
5154 
5155 static PyObject *
string_concatenate(PyObject * v,PyObject * w,PyFrameObject * f,unsigned char * next_instr)5156 string_concatenate(PyObject *v, PyObject *w,
5157                    PyFrameObject *f, unsigned char *next_instr)
5158 {
5159     /* This function implements 'variable += expr' when both arguments
5160        are strings. */
5161     Py_ssize_t v_len = PyString_GET_SIZE(v);
5162     Py_ssize_t w_len = PyString_GET_SIZE(w);
5163     Py_ssize_t new_len = v_len + w_len;
5164     if (new_len < 0) {
5165         PyErr_SetString(PyExc_OverflowError,
5166                         "strings are too large to concat");
5167         return NULL;
5168     }
5169 
5170     if (v->ob_refcnt == 2) {
5171         /* In the common case, there are 2 references to the value
5172          * stored in 'variable' when the += is performed: one on the
5173          * value stack (in 'v') and one still stored in the
5174          * 'variable'.  We try to delete the variable now to reduce
5175          * the refcnt to 1.
5176          */
5177         switch (*next_instr) {
5178         case STORE_FAST:
5179         {
5180             int oparg = PEEKARG();
5181             PyObject **fastlocals = f->f_localsplus;
5182             if (GETLOCAL(oparg) == v)
5183                 SETLOCAL(oparg, NULL);
5184             break;
5185         }
5186         case STORE_DEREF:
5187         {
5188             PyObject **freevars = (f->f_localsplus +
5189                                    f->f_code->co_nlocals);
5190             PyObject *c = freevars[PEEKARG()];
5191             if (PyCell_GET(c) == v)
5192                 PyCell_Set(c, NULL);
5193             break;
5194         }
5195         case STORE_NAME:
5196         {
5197             PyObject *names = f->f_code->co_names;
5198             PyObject *name = GETITEM(names, PEEKARG());
5199             PyObject *locals = f->f_locals;
5200             if (PyDict_CheckExact(locals) &&
5201                 PyDict_GetItem(locals, name) == v) {
5202                 if (PyDict_DelItem(locals, name) != 0) {
5203                     PyErr_Clear();
5204                 }
5205             }
5206             break;
5207         }
5208         }
5209     }
5210 
5211     if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
5212         /* Now we own the last reference to 'v', so we can resize it
5213          * in-place.
5214          */
5215         if (_PyString_Resize(&v, new_len) != 0) {
5216             /* XXX if _PyString_Resize() fails, 'v' has been
5217              * deallocated so it cannot be put back into
5218              * 'variable'.  The MemoryError is raised when there
5219              * is no value in 'variable', which might (very
5220              * remotely) be a cause of incompatibilities.
5221              */
5222             return NULL;
5223         }
5224         /* copy 'w' into the newly allocated area of 'v' */
5225         memcpy(PyString_AS_STRING(v) + v_len,
5226                PyString_AS_STRING(w), w_len);
5227         return v;
5228     }
5229     else {
5230         /* When in-place resizing is not an option. */
5231         PyString_Concat(&v, w);
5232         return v;
5233     }
5234 }
5235 
5236 #ifdef DYNAMIC_EXECUTION_PROFILE
5237 
5238 static PyObject *
getarray(long a[256])5239 getarray(long a[256])
5240 {
5241     int i;
5242     PyObject *l = PyList_New(256);
5243     if (l == NULL) return NULL;
5244     for (i = 0; i < 256; i++) {
5245         PyObject *x = PyInt_FromLong(a[i]);
5246         if (x == NULL) {
5247             Py_DECREF(l);
5248             return NULL;
5249         }
5250         PyList_SET_ITEM(l, i, x);
5251     }
5252     for (i = 0; i < 256; i++)
5253         a[i] = 0;
5254     return l;
5255 }
5256 
5257 PyObject *
_Py_GetDXProfile(PyObject * self,PyObject * args)5258 _Py_GetDXProfile(PyObject *self, PyObject *args)
5259 {
5260 #ifndef DXPAIRS
5261     return getarray(dxp);
5262 #else
5263     int i;
5264     PyObject *l = PyList_New(257);
5265     if (l == NULL) return NULL;
5266     for (i = 0; i < 257; i++) {
5267         PyObject *x = getarray(dxpairs[i]);
5268         if (x == NULL) {
5269             Py_DECREF(l);
5270             return NULL;
5271         }
5272         PyList_SET_ITEM(l, i, x);
5273     }
5274     return l;
5275 #endif
5276 }
5277 
5278 #endif
5279