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