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