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