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