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