ceval.c (104215B)
1 // XXX: [fabianw@seas.harvard.edu; 2022-11-08] This code has been shortened to 2 // fit the context of the lecture. The full code for this file can be found 3 // here: 4 // 5 // https://github.com/python/cpython/blob/v3.10.8/Python/ceval.c 6 7 PyObject* _Py_HOT_FUNCTION 8 _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag) 9 { 10 _Py_EnsureTstateNotNULL(tstate); 11 12 #if USE_COMPUTED_GOTOS 13 /* Import the static jump table */ 14 #include "opcode_targets.h" 15 #endif 16 17 #ifdef DXPAIRS 18 int lastopcode = 0; 19 #endif 20 PyObject **stack_pointer; /* Next free slot in value stack */ 21 const _Py_CODEUNIT *next_instr; 22 int opcode; /* Current opcode */ 23 int oparg; /* Current opcode argument, if any */ 24 PyObject **fastlocals, **freevars; 25 PyObject *retval = NULL; /* Return value */ 26 _Py_atomic_int * const eval_breaker = &tstate->interp->ceval.eval_breaker; 27 PyCodeObject *co; 28 29 const _Py_CODEUNIT *first_instr; 30 PyObject *names; 31 PyObject *consts; 32 _PyOpcache *co_opcache; 33 34 #ifdef LLTRACE 35 _Py_IDENTIFIER(__ltrace__); 36 #endif 37 38 if (_Py_EnterRecursiveCall(tstate, "")) { 39 return NULL; 40 } 41 42 PyTraceInfo trace_info; 43 /* Mark trace_info as uninitialized */ 44 trace_info.code = NULL; 45 46 /* WARNING: Because the CFrame lives on the C stack, 47 * but can be accessed from a heap allocated object (tstate) 48 * strict stack discipline must be maintained. 49 */ 50 CFrame *prev_cframe = tstate->cframe; 51 trace_info.cframe.use_tracing = prev_cframe->use_tracing; 52 trace_info.cframe.previous = prev_cframe; 53 tstate->cframe = &trace_info.cframe; 54 55 /* push frame */ 56 tstate->frame = f; 57 co = f->f_code; 58 59 if (trace_info.cframe.use_tracing) { 60 if (tstate->c_tracefunc != NULL) { 61 /* tstate->c_tracefunc, if defined, is a 62 function that will be called on *every* entry 63 to a code block. Its return value, if not 64 None, is a function that will be called at 65 the start of each executed line of code. 66 (Actually, the function must return itself 67 in order to continue tracing.) The trace 68 functions are called with three arguments: 69 a pointer to the current frame, a string 70 indicating why the function is called, and 71 an argument which depends on the situation. 72 The global trace function is also called 73 whenever an exception is detected. */ 74 if (call_trace_protected(tstate->c_tracefunc, 75 tstate->c_traceobj, 76 tstate, f, &trace_info, 77 PyTrace_CALL, Py_None)) { 78 /* Trace function raised an error */ 79 goto exit_eval_frame; 80 } 81 } 82 if (tstate->c_profilefunc != NULL) { 83 /* Similar for c_profilefunc, except it needn't 84 return itself and isn't called for "line" events */ 85 if (call_trace_protected(tstate->c_profilefunc, 86 tstate->c_profileobj, 87 tstate, f, &trace_info, 88 PyTrace_CALL, Py_None)) { 89 /* Profile function raised an error */ 90 goto exit_eval_frame; 91 } 92 } 93 } 94 95 if (PyDTrace_FUNCTION_ENTRY_ENABLED()) 96 dtrace_function_entry(f); 97 98 names = co->co_names; 99 consts = co->co_consts; 100 fastlocals = f->f_localsplus; 101 freevars = f->f_localsplus + co->co_nlocals; 102 assert(PyBytes_Check(co->co_code)); 103 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX); 104 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0); 105 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT))); 106 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code); 107 /* 108 f->f_lasti refers to the index of the last instruction, 109 unless it's -1 in which case next_instr should be first_instr. 110 111 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield 112 multiple values. 113 114 When the PREDICT() macros are enabled, some opcode pairs follow in 115 direct succession without updating f->f_lasti. A successful 116 prediction effectively links the two codes together as if they 117 were a single new opcode; accordingly,f->f_lasti will point to 118 the first code in the pair (for instance, GET_ITER followed by 119 FOR_ITER is effectively a single opcode and f->f_lasti will point 120 to the beginning of the combined pair.) 121 */ 122 assert(f->f_lasti >= -1); 123 next_instr = first_instr + f->f_lasti + 1; 124 stack_pointer = f->f_valuestack + f->f_stackdepth; 125 /* Set f->f_stackdepth to -1. 126 * Update when returning or calling trace function. 127 Having f_stackdepth <= 0 ensures that invalid 128 values are not visible to the cycle GC. 129 We choose -1 rather than 0 to assist debugging. 130 */ 131 f->f_stackdepth = -1; 132 f->f_state = FRAME_EXECUTING; 133 134 if (co->co_opcache_flag < opcache_min_runs) { 135 co->co_opcache_flag++; 136 if (co->co_opcache_flag == opcache_min_runs) { 137 if (_PyCode_InitOpcache(co) < 0) { 138 goto exit_eval_frame; 139 } 140 #if OPCACHE_STATS 141 opcache_code_objects_extra_mem += 142 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) + 143 sizeof(_PyOpcache) * co->co_opcache_size; 144 opcache_code_objects++; 145 #endif 146 } 147 } 148 149 #ifdef LLTRACE 150 { 151 int r = _PyDict_ContainsId(f->f_globals, &PyId___ltrace__); 152 if (r < 0) { 153 goto exit_eval_frame; 154 } 155 lltrace = r; 156 } 157 #endif 158 159 if (throwflag) { /* support for generator.throw() */ 160 goto error; 161 } 162 163 #ifdef Py_DEBUG 164 /* _PyEval_EvalFrameDefault() must not be called with an exception set, 165 because it can clear it (directly or indirectly) and so the 166 caller loses its exception */ 167 assert(!_PyErr_Occurred(tstate)); 168 #endif 169 170 main_loop: 171 for (;;) { 172 assert(stack_pointer >= f->f_valuestack); /* else underflow */ 173 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */ 174 assert(!_PyErr_Occurred(tstate)); 175 176 /* Do periodic things. Doing this every time through 177 the loop would add too much overhead, so we do it 178 only every Nth instruction. We also do it if 179 ``pending.calls_to_do'' is set, i.e. when an asynchronous 180 event needs attention (e.g. a signal handler or 181 async I/O handler); see Py_AddPendingCall() and 182 Py_MakePendingCalls() above. */ 183 184 if (_Py_atomic_load_relaxed(eval_breaker)) { 185 opcode = _Py_OPCODE(*next_instr); 186 if (opcode != SETUP_FINALLY && 187 opcode != SETUP_WITH && 188 opcode != BEFORE_ASYNC_WITH && 189 opcode != YIELD_FROM) { 190 /* Few cases where we skip running signal handlers and other 191 pending calls: 192 - If we're about to enter the 'with:'. It will prevent 193 emitting a resource warning in the common idiom 194 'with open(path) as file:'. 195 - If we're about to enter the 'async with:'. 196 - If we're about to enter the 'try:' of a try/finally (not 197 *very* useful, but might help in some cases and it's 198 traditional) 199 - If we're resuming a chain of nested 'yield from' or 200 'await' calls, then each frame is parked with YIELD_FROM 201 as its next opcode. If the user hit control-C we want to 202 wait until we've reached the innermost frame before 203 running the signal handler and raising KeyboardInterrupt 204 (see bpo-30039). 205 */ 206 if (eval_frame_handle_pending(tstate) != 0) { 207 goto error; 208 } 209 } 210 } 211 212 tracing_dispatch: 213 { 214 int instr_prev = f->f_lasti; 215 f->f_lasti = INSTR_OFFSET(); 216 NEXTOPARG(); 217 218 if (PyDTrace_LINE_ENABLED()) 219 maybe_dtrace_line(f, &trace_info, instr_prev); 220 221 /* line-by-line tracing support */ 222 223 if (trace_info.cframe.use_tracing && 224 tstate->c_tracefunc != NULL && !tstate->tracing) { 225 int err; 226 /* see maybe_call_line_trace() 227 for expository comments */ 228 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack); 229 230 err = maybe_call_line_trace(tstate->c_tracefunc, 231 tstate->c_traceobj, 232 tstate, f, 233 &trace_info, instr_prev); 234 /* Reload possibly changed frame fields */ 235 JUMPTO(f->f_lasti); 236 stack_pointer = f->f_valuestack+f->f_stackdepth; 237 f->f_stackdepth = -1; 238 if (err) { 239 /* trace function raised an exception */ 240 goto error; 241 } 242 NEXTOPARG(); 243 } 244 } 245 246 #ifdef LLTRACE 247 /* Instruction tracing */ 248 249 if (lltrace) { 250 if (HAS_ARG(opcode)) { 251 printf("%d: %d, %d\n", 252 f->f_lasti, opcode, oparg); 253 } 254 else { 255 printf("%d: %d\n", 256 f->f_lasti, opcode); 257 } 258 } 259 #endif 260 #if USE_COMPUTED_GOTOS == 0 261 goto dispatch_opcode; 262 263 predispatch: 264 if (trace_info.cframe.use_tracing OR_DTRACE_LINE OR_LLTRACE) { 265 goto tracing_dispatch; 266 } 267 f->f_lasti = INSTR_OFFSET(); 268 NEXTOPARG(); 269 #endif 270 dispatch_opcode: 271 #ifdef DYNAMIC_EXECUTION_PROFILE 272 #ifdef DXPAIRS 273 dxpairs[lastopcode][opcode]++; 274 lastopcode = opcode; 275 #endif 276 dxp[opcode]++; 277 #endif 278 279 switch (opcode) { 280 281 /* BEWARE! 282 It is essential that any operation that fails must goto error 283 and that all operation that succeed call DISPATCH() ! */ 284 285 case TARGET(NOP): { 286 DISPATCH(); 287 } 288 289 case TARGET(LOAD_FAST): { 290 PyObject *value = GETLOCAL(oparg); 291 if (value == NULL) { 292 format_exc_check_arg(tstate, PyExc_UnboundLocalError, 293 UNBOUNDLOCAL_ERROR_MSG, 294 PyTuple_GetItem(co->co_varnames, oparg)); 295 goto error; 296 } 297 Py_INCREF(value); 298 PUSH(value); 299 DISPATCH(); 300 } 301 302 case TARGET(LOAD_CONST): { 303 PREDICTED(LOAD_CONST); 304 PyObject *value = GETITEM(consts, oparg); 305 Py_INCREF(value); 306 PUSH(value); 307 DISPATCH(); 308 } 309 310 case TARGET(STORE_FAST): { 311 PREDICTED(STORE_FAST); 312 PyObject *value = POP(); 313 SETLOCAL(oparg, value); 314 DISPATCH(); 315 } 316 317 case TARGET(POP_TOP): { 318 PyObject *value = POP(); 319 Py_DECREF(value); 320 DISPATCH(); 321 } 322 323 case TARGET(ROT_TWO): { 324 PyObject *top = TOP(); 325 PyObject *second = SECOND(); 326 SET_TOP(second); 327 SET_SECOND(top); 328 DISPATCH(); 329 } 330 331 case TARGET(ROT_THREE): { 332 PyObject *top = TOP(); 333 PyObject *second = SECOND(); 334 PyObject *third = THIRD(); 335 SET_TOP(second); 336 SET_SECOND(third); 337 SET_THIRD(top); 338 DISPATCH(); 339 } 340 341 case TARGET(ROT_FOUR): { 342 PyObject *top = TOP(); 343 PyObject *second = SECOND(); 344 PyObject *third = THIRD(); 345 PyObject *fourth = FOURTH(); 346 SET_TOP(second); 347 SET_SECOND(third); 348 SET_THIRD(fourth); 349 SET_FOURTH(top); 350 DISPATCH(); 351 } 352 353 case TARGET(DUP_TOP): { 354 PyObject *top = TOP(); 355 Py_INCREF(top); 356 PUSH(top); 357 DISPATCH(); 358 } 359 360 case TARGET(DUP_TOP_TWO): { 361 PyObject *top = TOP(); 362 PyObject *second = SECOND(); 363 Py_INCREF(top); 364 Py_INCREF(second); 365 STACK_GROW(2); 366 SET_TOP(top); 367 SET_SECOND(second); 368 DISPATCH(); 369 } 370 371 case TARGET(UNARY_POSITIVE): { 372 PyObject *value = TOP(); 373 PyObject *res = PyNumber_Positive(value); 374 Py_DECREF(value); 375 SET_TOP(res); 376 if (res == NULL) 377 goto error; 378 DISPATCH(); 379 } 380 381 case TARGET(UNARY_NEGATIVE): { 382 PyObject *value = TOP(); 383 PyObject *res = PyNumber_Negative(value); 384 Py_DECREF(value); 385 SET_TOP(res); 386 if (res == NULL) 387 goto error; 388 DISPATCH(); 389 } 390 391 case TARGET(UNARY_NOT): { 392 PyObject *value = TOP(); 393 int err = PyObject_IsTrue(value); 394 Py_DECREF(value); 395 if (err == 0) { 396 Py_INCREF(Py_True); 397 SET_TOP(Py_True); 398 DISPATCH(); 399 } 400 else if (err > 0) { 401 Py_INCREF(Py_False); 402 SET_TOP(Py_False); 403 DISPATCH(); 404 } 405 STACK_SHRINK(1); 406 goto error; 407 } 408 409 case TARGET(UNARY_INVERT): { 410 PyObject *value = TOP(); 411 PyObject *res = PyNumber_Invert(value); 412 Py_DECREF(value); 413 SET_TOP(res); 414 if (res == NULL) 415 goto error; 416 DISPATCH(); 417 } 418 419 case TARGET(BINARY_POWER): { 420 PyObject *exp = POP(); 421 PyObject *base = TOP(); 422 PyObject *res = PyNumber_Power(base, exp, Py_None); 423 Py_DECREF(base); 424 Py_DECREF(exp); 425 SET_TOP(res); 426 if (res == NULL) 427 goto error; 428 DISPATCH(); 429 } 430 431 case TARGET(BINARY_MULTIPLY): { 432 PyObject *right = POP(); 433 PyObject *left = TOP(); 434 PyObject *res = PyNumber_Multiply(left, right); 435 Py_DECREF(left); 436 Py_DECREF(right); 437 SET_TOP(res); 438 if (res == NULL) 439 goto error; 440 DISPATCH(); 441 } 442 443 case TARGET(BINARY_MATRIX_MULTIPLY): { 444 PyObject *right = POP(); 445 PyObject *left = TOP(); 446 PyObject *res = PyNumber_MatrixMultiply(left, right); 447 Py_DECREF(left); 448 Py_DECREF(right); 449 SET_TOP(res); 450 if (res == NULL) 451 goto error; 452 DISPATCH(); 453 } 454 455 case TARGET(BINARY_TRUE_DIVIDE): { 456 PyObject *divisor = POP(); 457 PyObject *dividend = TOP(); 458 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor); 459 Py_DECREF(dividend); 460 Py_DECREF(divisor); 461 SET_TOP(quotient); 462 if (quotient == NULL) 463 goto error; 464 DISPATCH(); 465 } 466 467 case TARGET(BINARY_FLOOR_DIVIDE): { 468 PyObject *divisor = POP(); 469 PyObject *dividend = TOP(); 470 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor); 471 Py_DECREF(dividend); 472 Py_DECREF(divisor); 473 SET_TOP(quotient); 474 if (quotient == NULL) 475 goto error; 476 DISPATCH(); 477 } 478 479 case TARGET(BINARY_MODULO): { 480 PyObject *divisor = POP(); 481 PyObject *dividend = TOP(); 482 PyObject *res; 483 if (PyUnicode_CheckExact(dividend) && ( 484 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) { 485 // fast path; string formatting, but not if the RHS is a str subclass 486 // (see issue28598) 487 res = PyUnicode_Format(dividend, divisor); 488 } else { 489 res = PyNumber_Remainder(dividend, divisor); 490 } 491 Py_DECREF(divisor); 492 Py_DECREF(dividend); 493 SET_TOP(res); 494 if (res == NULL) 495 goto error; 496 DISPATCH(); 497 } 498 499 case TARGET(BINARY_ADD): { 500 PyObject *right = POP(); 501 PyObject *left = TOP(); 502 PyObject *sum; 503 /* NOTE(vstinner): Please don't try to micro-optimize int+int on 504 CPython using bytecode, it is simply worthless. 505 See http://bugs.python.org/issue21955 and 506 http://bugs.python.org/issue10044 for the discussion. In short, 507 no patch shown any impact on a realistic benchmark, only a minor 508 speedup on microbenchmarks. */ 509 if (PyUnicode_CheckExact(left) && 510 PyUnicode_CheckExact(right)) { 511 sum = unicode_concatenate(tstate, left, right, f, next_instr); 512 /* unicode_concatenate consumed the ref to left */ 513 } 514 else { 515 sum = PyNumber_Add(left, right); 516 Py_DECREF(left); 517 } 518 Py_DECREF(right); 519 SET_TOP(sum); 520 if (sum == NULL) 521 goto error; 522 DISPATCH(); 523 } 524 525 case TARGET(BINARY_SUBTRACT): { 526 PyObject *right = POP(); 527 PyObject *left = TOP(); 528 PyObject *diff = PyNumber_Subtract(left, right); 529 Py_DECREF(right); 530 Py_DECREF(left); 531 SET_TOP(diff); 532 if (diff == NULL) 533 goto error; 534 DISPATCH(); 535 } 536 537 case TARGET(BINARY_SUBSCR): { 538 PyObject *sub = POP(); 539 PyObject *container = TOP(); 540 PyObject *res = PyObject_GetItem(container, sub); 541 Py_DECREF(container); 542 Py_DECREF(sub); 543 SET_TOP(res); 544 if (res == NULL) 545 goto error; 546 DISPATCH(); 547 } 548 549 case TARGET(BINARY_LSHIFT): { 550 PyObject *right = POP(); 551 PyObject *left = TOP(); 552 PyObject *res = PyNumber_Lshift(left, right); 553 Py_DECREF(left); 554 Py_DECREF(right); 555 SET_TOP(res); 556 if (res == NULL) 557 goto error; 558 DISPATCH(); 559 } 560 561 case TARGET(BINARY_RSHIFT): { 562 PyObject *right = POP(); 563 PyObject *left = TOP(); 564 PyObject *res = PyNumber_Rshift(left, right); 565 Py_DECREF(left); 566 Py_DECREF(right); 567 SET_TOP(res); 568 if (res == NULL) 569 goto error; 570 DISPATCH(); 571 } 572 573 case TARGET(BINARY_AND): { 574 PyObject *right = POP(); 575 PyObject *left = TOP(); 576 PyObject *res = PyNumber_And(left, right); 577 Py_DECREF(left); 578 Py_DECREF(right); 579 SET_TOP(res); 580 if (res == NULL) 581 goto error; 582 DISPATCH(); 583 } 584 585 case TARGET(BINARY_XOR): { 586 PyObject *right = POP(); 587 PyObject *left = TOP(); 588 PyObject *res = PyNumber_Xor(left, right); 589 Py_DECREF(left); 590 Py_DECREF(right); 591 SET_TOP(res); 592 if (res == NULL) 593 goto error; 594 DISPATCH(); 595 } 596 597 case TARGET(BINARY_OR): { 598 PyObject *right = POP(); 599 PyObject *left = TOP(); 600 PyObject *res = PyNumber_Or(left, right); 601 Py_DECREF(left); 602 Py_DECREF(right); 603 SET_TOP(res); 604 if (res == NULL) 605 goto error; 606 DISPATCH(); 607 } 608 609 case TARGET(LIST_APPEND): { 610 PyObject *v = POP(); 611 PyObject *list = PEEK(oparg); 612 int err; 613 err = PyList_Append(list, v); 614 Py_DECREF(v); 615 if (err != 0) 616 goto error; 617 PREDICT(JUMP_ABSOLUTE); 618 DISPATCH(); 619 } 620 621 case TARGET(SET_ADD): { 622 PyObject *v = POP(); 623 PyObject *set = PEEK(oparg); 624 int err; 625 err = PySet_Add(set, v); 626 Py_DECREF(v); 627 if (err != 0) 628 goto error; 629 PREDICT(JUMP_ABSOLUTE); 630 DISPATCH(); 631 } 632 633 case TARGET(INPLACE_POWER): { 634 PyObject *exp = POP(); 635 PyObject *base = TOP(); 636 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None); 637 Py_DECREF(base); 638 Py_DECREF(exp); 639 SET_TOP(res); 640 if (res == NULL) 641 goto error; 642 DISPATCH(); 643 } 644 645 case TARGET(INPLACE_MULTIPLY): { 646 PyObject *right = POP(); 647 PyObject *left = TOP(); 648 PyObject *res = PyNumber_InPlaceMultiply(left, right); 649 Py_DECREF(left); 650 Py_DECREF(right); 651 SET_TOP(res); 652 if (res == NULL) 653 goto error; 654 DISPATCH(); 655 } 656 657 case TARGET(INPLACE_MATRIX_MULTIPLY): { 658 PyObject *right = POP(); 659 PyObject *left = TOP(); 660 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right); 661 Py_DECREF(left); 662 Py_DECREF(right); 663 SET_TOP(res); 664 if (res == NULL) 665 goto error; 666 DISPATCH(); 667 } 668 669 case TARGET(INPLACE_TRUE_DIVIDE): { 670 PyObject *divisor = POP(); 671 PyObject *dividend = TOP(); 672 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor); 673 Py_DECREF(dividend); 674 Py_DECREF(divisor); 675 SET_TOP(quotient); 676 if (quotient == NULL) 677 goto error; 678 DISPATCH(); 679 } 680 681 case TARGET(INPLACE_FLOOR_DIVIDE): { 682 PyObject *divisor = POP(); 683 PyObject *dividend = TOP(); 684 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor); 685 Py_DECREF(dividend); 686 Py_DECREF(divisor); 687 SET_TOP(quotient); 688 if (quotient == NULL) 689 goto error; 690 DISPATCH(); 691 } 692 693 case TARGET(INPLACE_MODULO): { 694 PyObject *right = POP(); 695 PyObject *left = TOP(); 696 PyObject *mod = PyNumber_InPlaceRemainder(left, right); 697 Py_DECREF(left); 698 Py_DECREF(right); 699 SET_TOP(mod); 700 if (mod == NULL) 701 goto error; 702 DISPATCH(); 703 } 704 705 case TARGET(INPLACE_ADD): { 706 PyObject *right = POP(); 707 PyObject *left = TOP(); 708 PyObject *sum; 709 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) { 710 sum = unicode_concatenate(tstate, left, right, f, next_instr); 711 /* unicode_concatenate consumed the ref to left */ 712 } 713 else { 714 sum = PyNumber_InPlaceAdd(left, right); 715 Py_DECREF(left); 716 } 717 Py_DECREF(right); 718 SET_TOP(sum); 719 if (sum == NULL) 720 goto error; 721 DISPATCH(); 722 } 723 724 case TARGET(INPLACE_SUBTRACT): { 725 PyObject *right = POP(); 726 PyObject *left = TOP(); 727 PyObject *diff = PyNumber_InPlaceSubtract(left, right); 728 Py_DECREF(left); 729 Py_DECREF(right); 730 SET_TOP(diff); 731 if (diff == NULL) 732 goto error; 733 DISPATCH(); 734 } 735 736 case TARGET(INPLACE_LSHIFT): { 737 PyObject *right = POP(); 738 PyObject *left = TOP(); 739 PyObject *res = PyNumber_InPlaceLshift(left, right); 740 Py_DECREF(left); 741 Py_DECREF(right); 742 SET_TOP(res); 743 if (res == NULL) 744 goto error; 745 DISPATCH(); 746 } 747 748 case TARGET(INPLACE_RSHIFT): { 749 PyObject *right = POP(); 750 PyObject *left = TOP(); 751 PyObject *res = PyNumber_InPlaceRshift(left, right); 752 Py_DECREF(left); 753 Py_DECREF(right); 754 SET_TOP(res); 755 if (res == NULL) 756 goto error; 757 DISPATCH(); 758 } 759 760 case TARGET(INPLACE_AND): { 761 PyObject *right = POP(); 762 PyObject *left = TOP(); 763 PyObject *res = PyNumber_InPlaceAnd(left, right); 764 Py_DECREF(left); 765 Py_DECREF(right); 766 SET_TOP(res); 767 if (res == NULL) 768 goto error; 769 DISPATCH(); 770 } 771 772 case TARGET(INPLACE_XOR): { 773 PyObject *right = POP(); 774 PyObject *left = TOP(); 775 PyObject *res = PyNumber_InPlaceXor(left, right); 776 Py_DECREF(left); 777 Py_DECREF(right); 778 SET_TOP(res); 779 if (res == NULL) 780 goto error; 781 DISPATCH(); 782 } 783 784 case TARGET(INPLACE_OR): { 785 PyObject *right = POP(); 786 PyObject *left = TOP(); 787 PyObject *res = PyNumber_InPlaceOr(left, right); 788 Py_DECREF(left); 789 Py_DECREF(right); 790 SET_TOP(res); 791 if (res == NULL) 792 goto error; 793 DISPATCH(); 794 } 795 796 case TARGET(STORE_SUBSCR): { 797 PyObject *sub = TOP(); 798 PyObject *container = SECOND(); 799 PyObject *v = THIRD(); 800 int err; 801 STACK_SHRINK(3); 802 /* container[sub] = v */ 803 err = PyObject_SetItem(container, sub, v); 804 Py_DECREF(v); 805 Py_DECREF(container); 806 Py_DECREF(sub); 807 if (err != 0) 808 goto error; 809 DISPATCH(); 810 } 811 812 case TARGET(DELETE_SUBSCR): { 813 PyObject *sub = TOP(); 814 PyObject *container = SECOND(); 815 int err; 816 STACK_SHRINK(2); 817 /* del container[sub] */ 818 err = PyObject_DelItem(container, sub); 819 Py_DECREF(container); 820 Py_DECREF(sub); 821 if (err != 0) 822 goto error; 823 DISPATCH(); 824 } 825 826 case TARGET(PRINT_EXPR): { 827 _Py_IDENTIFIER(displayhook); 828 PyObject *value = POP(); 829 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook); 830 PyObject *res; 831 if (hook == NULL) { 832 _PyErr_SetString(tstate, PyExc_RuntimeError, 833 "lost sys.displayhook"); 834 Py_DECREF(value); 835 goto error; 836 } 837 res = PyObject_CallOneArg(hook, value); 838 Py_DECREF(value); 839 if (res == NULL) 840 goto error; 841 Py_DECREF(res); 842 DISPATCH(); 843 } 844 845 case TARGET(RAISE_VARARGS): { 846 PyObject *cause = NULL, *exc = NULL; 847 switch (oparg) { 848 case 2: 849 cause = POP(); /* cause */ 850 /* fall through */ 851 case 1: 852 exc = POP(); /* exc */ 853 /* fall through */ 854 case 0: 855 if (do_raise(tstate, exc, cause)) { 856 goto exception_unwind; 857 } 858 break; 859 default: 860 _PyErr_SetString(tstate, PyExc_SystemError, 861 "bad RAISE_VARARGS oparg"); 862 break; 863 } 864 goto error; 865 } 866 867 case TARGET(RETURN_VALUE): { 868 retval = POP(); 869 assert(f->f_iblock == 0); 870 assert(EMPTY()); 871 f->f_state = FRAME_RETURNED; 872 f->f_stackdepth = 0; 873 goto exiting; 874 } 875 876 case TARGET(GET_AITER): { 877 unaryfunc getter = NULL; 878 PyObject *iter = NULL; 879 PyObject *obj = TOP(); 880 PyTypeObject *type = Py_TYPE(obj); 881 882 if (type->tp_as_async != NULL) { 883 getter = type->tp_as_async->am_aiter; 884 } 885 886 if (getter != NULL) { 887 iter = (*getter)(obj); 888 Py_DECREF(obj); 889 if (iter == NULL) { 890 SET_TOP(NULL); 891 goto error; 892 } 893 } 894 else { 895 SET_TOP(NULL); 896 _PyErr_Format(tstate, PyExc_TypeError, 897 "'async for' requires an object with " 898 "__aiter__ method, got %.100s", 899 type->tp_name); 900 Py_DECREF(obj); 901 goto error; 902 } 903 904 if (Py_TYPE(iter)->tp_as_async == NULL || 905 Py_TYPE(iter)->tp_as_async->am_anext == NULL) { 906 907 SET_TOP(NULL); 908 _PyErr_Format(tstate, PyExc_TypeError, 909 "'async for' received an object from __aiter__ " 910 "that does not implement __anext__: %.100s", 911 Py_TYPE(iter)->tp_name); 912 Py_DECREF(iter); 913 goto error; 914 } 915 916 SET_TOP(iter); 917 DISPATCH(); 918 } 919 920 case TARGET(GET_ANEXT): { 921 unaryfunc getter = NULL; 922 PyObject *next_iter = NULL; 923 PyObject *awaitable = NULL; 924 PyObject *aiter = TOP(); 925 PyTypeObject *type = Py_TYPE(aiter); 926 927 if (PyAsyncGen_CheckExact(aiter)) { 928 awaitable = type->tp_as_async->am_anext(aiter); 929 if (awaitable == NULL) { 930 goto error; 931 } 932 } else { 933 if (type->tp_as_async != NULL){ 934 getter = type->tp_as_async->am_anext; 935 } 936 937 if (getter != NULL) { 938 next_iter = (*getter)(aiter); 939 if (next_iter == NULL) { 940 goto error; 941 } 942 } 943 else { 944 _PyErr_Format(tstate, PyExc_TypeError, 945 "'async for' requires an iterator with " 946 "__anext__ method, got %.100s", 947 type->tp_name); 948 goto error; 949 } 950 951 awaitable = _PyCoro_GetAwaitableIter(next_iter); 952 if (awaitable == NULL) { 953 _PyErr_FormatFromCause( 954 PyExc_TypeError, 955 "'async for' received an invalid object " 956 "from __anext__: %.100s", 957 Py_TYPE(next_iter)->tp_name); 958 959 Py_DECREF(next_iter); 960 goto error; 961 } else { 962 Py_DECREF(next_iter); 963 } 964 } 965 966 PUSH(awaitable); 967 PREDICT(LOAD_CONST); 968 DISPATCH(); 969 } 970 971 case TARGET(GET_AWAITABLE): { 972 PREDICTED(GET_AWAITABLE); 973 PyObject *iterable = TOP(); 974 PyObject *iter = _PyCoro_GetAwaitableIter(iterable); 975 976 if (iter == NULL) { 977 int opcode_at_minus_3 = 0; 978 if ((next_instr - first_instr) > 2) { 979 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]); 980 } 981 format_awaitable_error(tstate, Py_TYPE(iterable), 982 opcode_at_minus_3, 983 _Py_OPCODE(next_instr[-2])); 984 } 985 986 Py_DECREF(iterable); 987 988 if (iter != NULL && PyCoro_CheckExact(iter)) { 989 PyObject *yf = _PyGen_yf((PyGenObject*)iter); 990 if (yf != NULL) { 991 /* `iter` is a coroutine object that is being 992 awaited, `yf` is a pointer to the current awaitable 993 being awaited on. */ 994 Py_DECREF(yf); 995 Py_CLEAR(iter); 996 _PyErr_SetString(tstate, PyExc_RuntimeError, 997 "coroutine is being awaited already"); 998 /* The code below jumps to `error` if `iter` is NULL. */ 999 } 1000 } 1001 1002 SET_TOP(iter); /* Even if it's NULL */ 1003 1004 if (iter == NULL) { 1005 goto error; 1006 } 1007 1008 PREDICT(LOAD_CONST); 1009 DISPATCH(); 1010 } 1011 1012 case TARGET(YIELD_FROM): { 1013 PyObject *v = POP(); 1014 PyObject *receiver = TOP(); 1015 PySendResult gen_status; 1016 if (tstate->c_tracefunc == NULL) { 1017 gen_status = PyIter_Send(receiver, v, &retval); 1018 } else { 1019 _Py_IDENTIFIER(send); 1020 if (Py_IsNone(v) && PyIter_Check(receiver)) { 1021 retval = Py_TYPE(receiver)->tp_iternext(receiver); 1022 } 1023 else { 1024 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v); 1025 } 1026 if (retval == NULL) { 1027 if (tstate->c_tracefunc != NULL 1028 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) 1029 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &trace_info); 1030 if (_PyGen_FetchStopIterationValue(&retval) == 0) { 1031 gen_status = PYGEN_RETURN; 1032 } 1033 else { 1034 gen_status = PYGEN_ERROR; 1035 } 1036 } 1037 else { 1038 gen_status = PYGEN_NEXT; 1039 } 1040 } 1041 Py_DECREF(v); 1042 if (gen_status == PYGEN_ERROR) { 1043 assert (retval == NULL); 1044 goto error; 1045 } 1046 if (gen_status == PYGEN_RETURN) { 1047 assert (retval != NULL); 1048 1049 Py_DECREF(receiver); 1050 SET_TOP(retval); 1051 retval = NULL; 1052 DISPATCH(); 1053 } 1054 assert (gen_status == PYGEN_NEXT); 1055 /* receiver remains on stack, retval is value to be yielded */ 1056 /* and repeat... */ 1057 assert(f->f_lasti > 0); 1058 f->f_lasti -= 1; 1059 f->f_state = FRAME_SUSPENDED; 1060 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack); 1061 goto exiting; 1062 } 1063 1064 case TARGET(YIELD_VALUE): { 1065 retval = POP(); 1066 1067 if (co->co_flags & CO_ASYNC_GENERATOR) { 1068 PyObject *w = _PyAsyncGenValueWrapperNew(retval); 1069 Py_DECREF(retval); 1070 if (w == NULL) { 1071 retval = NULL; 1072 goto error; 1073 } 1074 retval = w; 1075 } 1076 f->f_state = FRAME_SUSPENDED; 1077 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack); 1078 goto exiting; 1079 } 1080 1081 case TARGET(GEN_START): { 1082 PyObject *none = POP(); 1083 assert(none == Py_None); 1084 assert(oparg < 3); 1085 Py_DECREF(none); 1086 DISPATCH(); 1087 } 1088 1089 case TARGET(POP_EXCEPT): { 1090 PyObject *type, *value, *traceback; 1091 _PyErr_StackItem *exc_info; 1092 PyTryBlock *b = PyFrame_BlockPop(f); 1093 if (b->b_type != EXCEPT_HANDLER) { 1094 _PyErr_SetString(tstate, PyExc_SystemError, 1095 "popped block is not an except handler"); 1096 goto error; 1097 } 1098 assert(STACK_LEVEL() >= (b)->b_level + 3 && 1099 STACK_LEVEL() <= (b)->b_level + 4); 1100 exc_info = tstate->exc_info; 1101 type = exc_info->exc_type; 1102 value = exc_info->exc_value; 1103 traceback = exc_info->exc_traceback; 1104 exc_info->exc_type = POP(); 1105 exc_info->exc_value = POP(); 1106 exc_info->exc_traceback = POP(); 1107 Py_XDECREF(type); 1108 Py_XDECREF(value); 1109 Py_XDECREF(traceback); 1110 DISPATCH(); 1111 } 1112 1113 case TARGET(POP_BLOCK): { 1114 PyFrame_BlockPop(f); 1115 DISPATCH(); 1116 } 1117 1118 case TARGET(RERAISE): { 1119 assert(f->f_iblock > 0); 1120 if (oparg) { 1121 f->f_lasti = f->f_blockstack[f->f_iblock-1].b_handler; 1122 } 1123 PyObject *exc = POP(); 1124 PyObject *val = POP(); 1125 PyObject *tb = POP(); 1126 assert(PyExceptionClass_Check(exc)); 1127 _PyErr_Restore(tstate, exc, val, tb); 1128 goto exception_unwind; 1129 } 1130 1131 case TARGET(END_ASYNC_FOR): { 1132 PyObject *exc = POP(); 1133 assert(PyExceptionClass_Check(exc)); 1134 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) { 1135 PyTryBlock *b = PyFrame_BlockPop(f); 1136 assert(b->b_type == EXCEPT_HANDLER); 1137 Py_DECREF(exc); 1138 UNWIND_EXCEPT_HANDLER(b); 1139 Py_DECREF(POP()); 1140 JUMPBY(oparg); 1141 DISPATCH(); 1142 } 1143 else { 1144 PyObject *val = POP(); 1145 PyObject *tb = POP(); 1146 _PyErr_Restore(tstate, exc, val, tb); 1147 goto exception_unwind; 1148 } 1149 } 1150 1151 case TARGET(LOAD_ASSERTION_ERROR): { 1152 PyObject *value = PyExc_AssertionError; 1153 Py_INCREF(value); 1154 PUSH(value); 1155 DISPATCH(); 1156 } 1157 1158 case TARGET(LOAD_BUILD_CLASS): { 1159 _Py_IDENTIFIER(__build_class__); 1160 1161 PyObject *bc; 1162 if (PyDict_CheckExact(f->f_builtins)) { 1163 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__); 1164 if (bc == NULL) { 1165 if (!_PyErr_Occurred(tstate)) { 1166 _PyErr_SetString(tstate, PyExc_NameError, 1167 "__build_class__ not found"); 1168 } 1169 goto error; 1170 } 1171 Py_INCREF(bc); 1172 } 1173 else { 1174 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__); 1175 if (build_class_str == NULL) 1176 goto error; 1177 bc = PyObject_GetItem(f->f_builtins, build_class_str); 1178 if (bc == NULL) { 1179 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) 1180 _PyErr_SetString(tstate, PyExc_NameError, 1181 "__build_class__ not found"); 1182 goto error; 1183 } 1184 } 1185 PUSH(bc); 1186 DISPATCH(); 1187 } 1188 1189 case TARGET(STORE_NAME): { 1190 PyObject *name = GETITEM(names, oparg); 1191 PyObject *v = POP(); 1192 PyObject *ns = f->f_locals; 1193 int err; 1194 if (ns == NULL) { 1195 _PyErr_Format(tstate, PyExc_SystemError, 1196 "no locals found when storing %R", name); 1197 Py_DECREF(v); 1198 goto error; 1199 } 1200 if (PyDict_CheckExact(ns)) 1201 err = PyDict_SetItem(ns, name, v); 1202 else 1203 err = PyObject_SetItem(ns, name, v); 1204 Py_DECREF(v); 1205 if (err != 0) 1206 goto error; 1207 DISPATCH(); 1208 } 1209 1210 case TARGET(DELETE_NAME): { 1211 PyObject *name = GETITEM(names, oparg); 1212 PyObject *ns = f->f_locals; 1213 int err; 1214 if (ns == NULL) { 1215 _PyErr_Format(tstate, PyExc_SystemError, 1216 "no locals when deleting %R", name); 1217 goto error; 1218 } 1219 err = PyObject_DelItem(ns, name); 1220 if (err != 0) { 1221 format_exc_check_arg(tstate, PyExc_NameError, 1222 NAME_ERROR_MSG, 1223 name); 1224 goto error; 1225 } 1226 DISPATCH(); 1227 } 1228 1229 case TARGET(UNPACK_SEQUENCE): { 1230 PREDICTED(UNPACK_SEQUENCE); 1231 PyObject *seq = POP(), *item, **items; 1232 if (PyTuple_CheckExact(seq) && 1233 PyTuple_GET_SIZE(seq) == oparg) { 1234 items = ((PyTupleObject *)seq)->ob_item; 1235 while (oparg--) { 1236 item = items[oparg]; 1237 Py_INCREF(item); 1238 PUSH(item); 1239 } 1240 } else if (PyList_CheckExact(seq) && 1241 PyList_GET_SIZE(seq) == oparg) { 1242 items = ((PyListObject *)seq)->ob_item; 1243 while (oparg--) { 1244 item = items[oparg]; 1245 Py_INCREF(item); 1246 PUSH(item); 1247 } 1248 } else if (unpack_iterable(tstate, seq, oparg, -1, 1249 stack_pointer + oparg)) { 1250 STACK_GROW(oparg); 1251 } else { 1252 /* unpack_iterable() raised an exception */ 1253 Py_DECREF(seq); 1254 goto error; 1255 } 1256 Py_DECREF(seq); 1257 DISPATCH(); 1258 } 1259 1260 case TARGET(UNPACK_EX): { 1261 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8); 1262 PyObject *seq = POP(); 1263 1264 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8, 1265 stack_pointer + totalargs)) { 1266 stack_pointer += totalargs; 1267 } else { 1268 Py_DECREF(seq); 1269 goto error; 1270 } 1271 Py_DECREF(seq); 1272 DISPATCH(); 1273 } 1274 1275 case TARGET(STORE_ATTR): { 1276 PyObject *name = GETITEM(names, oparg); 1277 PyObject *owner = TOP(); 1278 PyObject *v = SECOND(); 1279 int err; 1280 STACK_SHRINK(2); 1281 err = PyObject_SetAttr(owner, name, v); 1282 Py_DECREF(v); 1283 Py_DECREF(owner); 1284 if (err != 0) 1285 goto error; 1286 DISPATCH(); 1287 } 1288 1289 case TARGET(DELETE_ATTR): { 1290 PyObject *name = GETITEM(names, oparg); 1291 PyObject *owner = POP(); 1292 int err; 1293 err = PyObject_SetAttr(owner, name, (PyObject *)NULL); 1294 Py_DECREF(owner); 1295 if (err != 0) 1296 goto error; 1297 DISPATCH(); 1298 } 1299 1300 case TARGET(STORE_GLOBAL): { 1301 PyObject *name = GETITEM(names, oparg); 1302 PyObject *v = POP(); 1303 int err; 1304 err = PyDict_SetItem(f->f_globals, name, v); 1305 Py_DECREF(v); 1306 if (err != 0) 1307 goto error; 1308 DISPATCH(); 1309 } 1310 1311 case TARGET(DELETE_GLOBAL): { 1312 PyObject *name = GETITEM(names, oparg); 1313 int err; 1314 err = PyDict_DelItem(f->f_globals, name); 1315 if (err != 0) { 1316 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { 1317 format_exc_check_arg(tstate, PyExc_NameError, 1318 NAME_ERROR_MSG, name); 1319 } 1320 goto error; 1321 } 1322 DISPATCH(); 1323 } 1324 1325 case TARGET(LOAD_NAME): { 1326 PyObject *name = GETITEM(names, oparg); 1327 PyObject *locals = f->f_locals; 1328 PyObject *v; 1329 if (locals == NULL) { 1330 _PyErr_Format(tstate, PyExc_SystemError, 1331 "no locals when loading %R", name); 1332 goto error; 1333 } 1334 if (PyDict_CheckExact(locals)) { 1335 v = PyDict_GetItemWithError(locals, name); 1336 if (v != NULL) { 1337 Py_INCREF(v); 1338 } 1339 else if (_PyErr_Occurred(tstate)) { 1340 goto error; 1341 } 1342 } 1343 else { 1344 v = PyObject_GetItem(locals, name); 1345 if (v == NULL) { 1346 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) 1347 goto error; 1348 _PyErr_Clear(tstate); 1349 } 1350 } 1351 if (v == NULL) { 1352 v = PyDict_GetItemWithError(f->f_globals, name); 1353 if (v != NULL) { 1354 Py_INCREF(v); 1355 } 1356 else if (_PyErr_Occurred(tstate)) { 1357 goto error; 1358 } 1359 else { 1360 if (PyDict_CheckExact(f->f_builtins)) { 1361 v = PyDict_GetItemWithError(f->f_builtins, name); 1362 if (v == NULL) { 1363 if (!_PyErr_Occurred(tstate)) { 1364 format_exc_check_arg( 1365 tstate, PyExc_NameError, 1366 NAME_ERROR_MSG, name); 1367 } 1368 goto error; 1369 } 1370 Py_INCREF(v); 1371 } 1372 else { 1373 v = PyObject_GetItem(f->f_builtins, name); 1374 if (v == NULL) { 1375 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { 1376 format_exc_check_arg( 1377 tstate, PyExc_NameError, 1378 NAME_ERROR_MSG, name); 1379 } 1380 goto error; 1381 } 1382 } 1383 } 1384 } 1385 PUSH(v); 1386 DISPATCH(); 1387 } 1388 1389 case TARGET(LOAD_GLOBAL): { 1390 PyObject *name; 1391 PyObject *v; 1392 if (PyDict_CheckExact(f->f_globals) 1393 && PyDict_CheckExact(f->f_builtins)) 1394 { 1395 OPCACHE_CHECK(); 1396 if (co_opcache != NULL && co_opcache->optimized > 0) { 1397 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg; 1398 1399 if (lg->globals_ver == 1400 ((PyDictObject *)f->f_globals)->ma_version_tag 1401 && lg->builtins_ver == 1402 ((PyDictObject *)f->f_builtins)->ma_version_tag) 1403 { 1404 PyObject *ptr = lg->ptr; 1405 OPCACHE_STAT_GLOBAL_HIT(); 1406 assert(ptr != NULL); 1407 Py_INCREF(ptr); 1408 PUSH(ptr); 1409 DISPATCH(); 1410 } 1411 } 1412 1413 name = GETITEM(names, oparg); 1414 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals, 1415 (PyDictObject *)f->f_builtins, 1416 name); 1417 if (v == NULL) { 1418 if (!_PyErr_Occurred(tstate)) { 1419 /* _PyDict_LoadGlobal() returns NULL without raising 1420 * an exception if the key doesn't exist */ 1421 format_exc_check_arg(tstate, PyExc_NameError, 1422 NAME_ERROR_MSG, name); 1423 } 1424 goto error; 1425 } 1426 1427 if (co_opcache != NULL) { 1428 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg; 1429 1430 if (co_opcache->optimized == 0) { 1431 /* Wasn't optimized before. */ 1432 OPCACHE_STAT_GLOBAL_OPT(); 1433 } else { 1434 OPCACHE_STAT_GLOBAL_MISS(); 1435 } 1436 1437 co_opcache->optimized = 1; 1438 lg->globals_ver = 1439 ((PyDictObject *)f->f_globals)->ma_version_tag; 1440 lg->builtins_ver = 1441 ((PyDictObject *)f->f_builtins)->ma_version_tag; 1442 lg->ptr = v; /* borrowed */ 1443 } 1444 1445 Py_INCREF(v); 1446 } 1447 else { 1448 /* Slow-path if globals or builtins is not a dict */ 1449 1450 /* namespace 1: globals */ 1451 name = GETITEM(names, oparg); 1452 v = PyObject_GetItem(f->f_globals, name); 1453 if (v == NULL) { 1454 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { 1455 goto error; 1456 } 1457 _PyErr_Clear(tstate); 1458 1459 /* namespace 2: builtins */ 1460 v = PyObject_GetItem(f->f_builtins, name); 1461 if (v == NULL) { 1462 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { 1463 format_exc_check_arg( 1464 tstate, PyExc_NameError, 1465 NAME_ERROR_MSG, name); 1466 } 1467 goto error; 1468 } 1469 } 1470 } 1471 PUSH(v); 1472 DISPATCH(); 1473 } 1474 1475 case TARGET(DELETE_FAST): { 1476 PyObject *v = GETLOCAL(oparg); 1477 if (v != NULL) { 1478 SETLOCAL(oparg, NULL); 1479 DISPATCH(); 1480 } 1481 format_exc_check_arg( 1482 tstate, PyExc_UnboundLocalError, 1483 UNBOUNDLOCAL_ERROR_MSG, 1484 PyTuple_GetItem(co->co_varnames, oparg) 1485 ); 1486 goto error; 1487 } 1488 1489 case TARGET(DELETE_DEREF): { 1490 PyObject *cell = freevars[oparg]; 1491 PyObject *oldobj = PyCell_GET(cell); 1492 if (oldobj != NULL) { 1493 PyCell_SET(cell, NULL); 1494 Py_DECREF(oldobj); 1495 DISPATCH(); 1496 } 1497 format_exc_unbound(tstate, co, oparg); 1498 goto error; 1499 } 1500 1501 case TARGET(LOAD_CLOSURE): { 1502 PyObject *cell = freevars[oparg]; 1503 Py_INCREF(cell); 1504 PUSH(cell); 1505 DISPATCH(); 1506 } 1507 1508 case TARGET(LOAD_CLASSDEREF): { 1509 PyObject *name, *value, *locals = f->f_locals; 1510 Py_ssize_t idx; 1511 assert(locals); 1512 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars)); 1513 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars); 1514 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars)); 1515 name = PyTuple_GET_ITEM(co->co_freevars, idx); 1516 if (PyDict_CheckExact(locals)) { 1517 value = PyDict_GetItemWithError(locals, name); 1518 if (value != NULL) { 1519 Py_INCREF(value); 1520 } 1521 else if (_PyErr_Occurred(tstate)) { 1522 goto error; 1523 } 1524 } 1525 else { 1526 value = PyObject_GetItem(locals, name); 1527 if (value == NULL) { 1528 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { 1529 goto error; 1530 } 1531 _PyErr_Clear(tstate); 1532 } 1533 } 1534 if (!value) { 1535 PyObject *cell = freevars[oparg]; 1536 value = PyCell_GET(cell); 1537 if (value == NULL) { 1538 format_exc_unbound(tstate, co, oparg); 1539 goto error; 1540 } 1541 Py_INCREF(value); 1542 } 1543 PUSH(value); 1544 DISPATCH(); 1545 } 1546 1547 case TARGET(LOAD_DEREF): { 1548 PyObject *cell = freevars[oparg]; 1549 PyObject *value = PyCell_GET(cell); 1550 if (value == NULL) { 1551 format_exc_unbound(tstate, co, oparg); 1552 goto error; 1553 } 1554 Py_INCREF(value); 1555 PUSH(value); 1556 DISPATCH(); 1557 } 1558 1559 case TARGET(STORE_DEREF): { 1560 PyObject *v = POP(); 1561 PyObject *cell = freevars[oparg]; 1562 PyObject *oldobj = PyCell_GET(cell); 1563 PyCell_SET(cell, v); 1564 Py_XDECREF(oldobj); 1565 DISPATCH(); 1566 } 1567 1568 case TARGET(BUILD_STRING): { 1569 PyObject *str; 1570 PyObject *empty = PyUnicode_New(0, 0); 1571 if (empty == NULL) { 1572 goto error; 1573 } 1574 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg); 1575 Py_DECREF(empty); 1576 if (str == NULL) 1577 goto error; 1578 while (--oparg >= 0) { 1579 PyObject *item = POP(); 1580 Py_DECREF(item); 1581 } 1582 PUSH(str); 1583 DISPATCH(); 1584 } 1585 1586 case TARGET(BUILD_TUPLE): { 1587 PyObject *tup = PyTuple_New(oparg); 1588 if (tup == NULL) 1589 goto error; 1590 while (--oparg >= 0) { 1591 PyObject *item = POP(); 1592 PyTuple_SET_ITEM(tup, oparg, item); 1593 } 1594 PUSH(tup); 1595 DISPATCH(); 1596 } 1597 1598 case TARGET(BUILD_LIST): { 1599 PyObject *list = PyList_New(oparg); 1600 if (list == NULL) 1601 goto error; 1602 while (--oparg >= 0) { 1603 PyObject *item = POP(); 1604 PyList_SET_ITEM(list, oparg, item); 1605 } 1606 PUSH(list); 1607 DISPATCH(); 1608 } 1609 1610 case TARGET(LIST_TO_TUPLE): { 1611 PyObject *list = POP(); 1612 PyObject *tuple = PyList_AsTuple(list); 1613 Py_DECREF(list); 1614 if (tuple == NULL) { 1615 goto error; 1616 } 1617 PUSH(tuple); 1618 DISPATCH(); 1619 } 1620 1621 case TARGET(LIST_EXTEND): { 1622 PyObject *iterable = POP(); 1623 PyObject *list = PEEK(oparg); 1624 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable); 1625 if (none_val == NULL) { 1626 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) && 1627 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable))) 1628 { 1629 _PyErr_Clear(tstate); 1630 _PyErr_Format(tstate, PyExc_TypeError, 1631 "Value after * must be an iterable, not %.200s", 1632 Py_TYPE(iterable)->tp_name); 1633 } 1634 Py_DECREF(iterable); 1635 goto error; 1636 } 1637 Py_DECREF(none_val); 1638 Py_DECREF(iterable); 1639 DISPATCH(); 1640 } 1641 1642 case TARGET(SET_UPDATE): { 1643 PyObject *iterable = POP(); 1644 PyObject *set = PEEK(oparg); 1645 int err = _PySet_Update(set, iterable); 1646 Py_DECREF(iterable); 1647 if (err < 0) { 1648 goto error; 1649 } 1650 DISPATCH(); 1651 } 1652 1653 case TARGET(BUILD_SET): { 1654 PyObject *set = PySet_New(NULL); 1655 int err = 0; 1656 int i; 1657 if (set == NULL) 1658 goto error; 1659 for (i = oparg; i > 0; i--) { 1660 PyObject *item = PEEK(i); 1661 if (err == 0) 1662 err = PySet_Add(set, item); 1663 Py_DECREF(item); 1664 } 1665 STACK_SHRINK(oparg); 1666 if (err != 0) { 1667 Py_DECREF(set); 1668 goto error; 1669 } 1670 PUSH(set); 1671 DISPATCH(); 1672 } 1673 1674 case TARGET(BUILD_MAP): { 1675 Py_ssize_t i; 1676 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg); 1677 if (map == NULL) 1678 goto error; 1679 for (i = oparg; i > 0; i--) { 1680 int err; 1681 PyObject *key = PEEK(2*i); 1682 PyObject *value = PEEK(2*i - 1); 1683 err = PyDict_SetItem(map, key, value); 1684 if (err != 0) { 1685 Py_DECREF(map); 1686 goto error; 1687 } 1688 } 1689 1690 while (oparg--) { 1691 Py_DECREF(POP()); 1692 Py_DECREF(POP()); 1693 } 1694 PUSH(map); 1695 DISPATCH(); 1696 } 1697 1698 case TARGET(SETUP_ANNOTATIONS): { 1699 _Py_IDENTIFIER(__annotations__); 1700 int err; 1701 PyObject *ann_dict; 1702 if (f->f_locals == NULL) { 1703 _PyErr_Format(tstate, PyExc_SystemError, 1704 "no locals found when setting up annotations"); 1705 goto error; 1706 } 1707 /* check if __annotations__ in locals()... */ 1708 if (PyDict_CheckExact(f->f_locals)) { 1709 ann_dict = _PyDict_GetItemIdWithError(f->f_locals, 1710 &PyId___annotations__); 1711 if (ann_dict == NULL) { 1712 if (_PyErr_Occurred(tstate)) { 1713 goto error; 1714 } 1715 /* ...if not, create a new one */ 1716 ann_dict = PyDict_New(); 1717 if (ann_dict == NULL) { 1718 goto error; 1719 } 1720 err = _PyDict_SetItemId(f->f_locals, 1721 &PyId___annotations__, ann_dict); 1722 Py_DECREF(ann_dict); 1723 if (err != 0) { 1724 goto error; 1725 } 1726 } 1727 } 1728 else { 1729 /* do the same if locals() is not a dict */ 1730 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__); 1731 if (ann_str == NULL) { 1732 goto error; 1733 } 1734 ann_dict = PyObject_GetItem(f->f_locals, ann_str); 1735 if (ann_dict == NULL) { 1736 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { 1737 goto error; 1738 } 1739 _PyErr_Clear(tstate); 1740 ann_dict = PyDict_New(); 1741 if (ann_dict == NULL) { 1742 goto error; 1743 } 1744 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict); 1745 Py_DECREF(ann_dict); 1746 if (err != 0) { 1747 goto error; 1748 } 1749 } 1750 else { 1751 Py_DECREF(ann_dict); 1752 } 1753 } 1754 DISPATCH(); 1755 } 1756 1757 case TARGET(BUILD_CONST_KEY_MAP): { 1758 Py_ssize_t i; 1759 PyObject *map; 1760 PyObject *keys = TOP(); 1761 if (!PyTuple_CheckExact(keys) || 1762 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) { 1763 _PyErr_SetString(tstate, PyExc_SystemError, 1764 "bad BUILD_CONST_KEY_MAP keys argument"); 1765 goto error; 1766 } 1767 map = _PyDict_NewPresized((Py_ssize_t)oparg); 1768 if (map == NULL) { 1769 goto error; 1770 } 1771 for (i = oparg; i > 0; i--) { 1772 int err; 1773 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i); 1774 PyObject *value = PEEK(i + 1); 1775 err = PyDict_SetItem(map, key, value); 1776 if (err != 0) { 1777 Py_DECREF(map); 1778 goto error; 1779 } 1780 } 1781 1782 Py_DECREF(POP()); 1783 while (oparg--) { 1784 Py_DECREF(POP()); 1785 } 1786 PUSH(map); 1787 DISPATCH(); 1788 } 1789 1790 case TARGET(DICT_UPDATE): { 1791 PyObject *update = POP(); 1792 PyObject *dict = PEEK(oparg); 1793 if (PyDict_Update(dict, update) < 0) { 1794 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) { 1795 _PyErr_Format(tstate, PyExc_TypeError, 1796 "'%.200s' object is not a mapping", 1797 Py_TYPE(update)->tp_name); 1798 } 1799 Py_DECREF(update); 1800 goto error; 1801 } 1802 Py_DECREF(update); 1803 DISPATCH(); 1804 } 1805 1806 case TARGET(DICT_MERGE): { 1807 PyObject *update = POP(); 1808 PyObject *dict = PEEK(oparg); 1809 1810 if (_PyDict_MergeEx(dict, update, 2) < 0) { 1811 format_kwargs_error(tstate, PEEK(2 + oparg), update); 1812 Py_DECREF(update); 1813 goto error; 1814 } 1815 Py_DECREF(update); 1816 PREDICT(CALL_FUNCTION_EX); 1817 DISPATCH(); 1818 } 1819 1820 case TARGET(MAP_ADD): { 1821 PyObject *value = TOP(); 1822 PyObject *key = SECOND(); 1823 PyObject *map; 1824 int err; 1825 STACK_SHRINK(2); 1826 map = PEEK(oparg); /* dict */ 1827 assert(PyDict_CheckExact(map)); 1828 err = PyDict_SetItem(map, key, value); /* map[key] = value */ 1829 Py_DECREF(value); 1830 Py_DECREF(key); 1831 if (err != 0) 1832 goto error; 1833 PREDICT(JUMP_ABSOLUTE); 1834 DISPATCH(); 1835 } 1836 1837 case TARGET(LOAD_ATTR): { 1838 PyObject *name = GETITEM(names, oparg); 1839 PyObject *owner = TOP(); 1840 1841 PyTypeObject *type = Py_TYPE(owner); 1842 PyObject *res; 1843 PyObject **dictptr; 1844 PyObject *dict; 1845 _PyOpCodeOpt_LoadAttr *la; 1846 1847 OPCACHE_STAT_ATTR_TOTAL(); 1848 1849 OPCACHE_CHECK(); 1850 if (co_opcache != NULL && PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) 1851 { 1852 if (co_opcache->optimized > 0) { 1853 // Fast path -- cache hit makes LOAD_ATTR ~30% faster. 1854 la = &co_opcache->u.la; 1855 if (la->type == type && la->tp_version_tag == type->tp_version_tag) 1856 { 1857 // Hint >= 0 is a dict index; hint == -1 is a dict miss. 1858 // Hint < -1 is an inverted slot offset: offset is strictly > 0, 1859 // so ~offset is strictly < -1 (assuming 2's complement). 1860 if (la->hint < -1) { 1861 // Even faster path -- slot hint. 1862 Py_ssize_t offset = ~la->hint; 1863 // fprintf(stderr, "Using hint for offset %zd\n", offset); 1864 char *addr = (char *)owner + offset; 1865 res = *(PyObject **)addr; 1866 if (res != NULL) { 1867 Py_INCREF(res); 1868 SET_TOP(res); 1869 Py_DECREF(owner); 1870 DISPATCH(); 1871 } 1872 // Else slot is NULL. Fall through to slow path to raise AttributeError(name). 1873 // Don't DEOPT, since the slot is still there. 1874 } else { 1875 // Fast path for dict. 1876 assert(type->tp_dict != NULL); 1877 assert(type->tp_dictoffset > 0); 1878 1879 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset); 1880 dict = *dictptr; 1881 if (dict != NULL && PyDict_CheckExact(dict)) { 1882 Py_ssize_t hint = la->hint; 1883 Py_INCREF(dict); 1884 res = NULL; 1885 assert(!_PyErr_Occurred(tstate)); 1886 la->hint = _PyDict_GetItemHint((PyDictObject*)dict, name, hint, &res); 1887 if (res != NULL) { 1888 assert(la->hint >= 0); 1889 if (la->hint == hint && hint >= 0) { 1890 // Our hint has helped -- cache hit. 1891 OPCACHE_STAT_ATTR_HIT(); 1892 } else { 1893 // The hint we provided didn't work. 1894 // Maybe next time? 1895 OPCACHE_MAYBE_DEOPT_LOAD_ATTR(); 1896 } 1897 1898 Py_INCREF(res); 1899 SET_TOP(res); 1900 Py_DECREF(owner); 1901 Py_DECREF(dict); 1902 DISPATCH(); 1903 } 1904 else { 1905 _PyErr_Clear(tstate); 1906 // This attribute can be missing sometimes; 1907 // we don't want to optimize this lookup. 1908 OPCACHE_DEOPT_LOAD_ATTR(); 1909 Py_DECREF(dict); 1910 } 1911 } 1912 else { 1913 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact. 1914 OPCACHE_DEOPT_LOAD_ATTR(); 1915 } 1916 } 1917 } 1918 else { 1919 // The type of the object has either been updated, 1920 // or is different. Maybe it will stabilize? 1921 OPCACHE_MAYBE_DEOPT_LOAD_ATTR(); 1922 } 1923 OPCACHE_STAT_ATTR_MISS(); 1924 } 1925 1926 if (co_opcache != NULL && // co_opcache can be NULL after a DEOPT() call. 1927 type->tp_getattro == PyObject_GenericGetAttr) 1928 { 1929 if (type->tp_dict == NULL) { 1930 if (PyType_Ready(type) < 0) { 1931 Py_DECREF(owner); 1932 SET_TOP(NULL); 1933 goto error; 1934 } 1935 } 1936 PyObject *descr = _PyType_Lookup(type, name); 1937 if (descr != NULL) { 1938 // We found an attribute with a data-like descriptor. 1939 PyTypeObject *dtype = Py_TYPE(descr); 1940 if (dtype == &PyMemberDescr_Type) { // It's a slot 1941 PyMemberDescrObject *member = (PyMemberDescrObject *)descr; 1942 struct PyMemberDef *dmem = member->d_member; 1943 if (dmem->type == T_OBJECT_EX) { 1944 Py_ssize_t offset = dmem->offset; 1945 assert(offset > 0); // 0 would be confused with dict hint == -1 (miss). 1946 1947 if (co_opcache->optimized == 0) { 1948 // First time we optimize this opcode. 1949 OPCACHE_STAT_ATTR_OPT(); 1950 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES; 1951 // fprintf(stderr, "Setting hint for %s, offset %zd\n", dmem->name, offset); 1952 } 1953 1954 la = &co_opcache->u.la; 1955 la->type = type; 1956 la->tp_version_tag = type->tp_version_tag; 1957 la->hint = ~offset; 1958 1959 char *addr = (char *)owner + offset; 1960 res = *(PyObject **)addr; 1961 if (res != NULL) { 1962 Py_INCREF(res); 1963 Py_DECREF(owner); 1964 SET_TOP(res); 1965 1966 DISPATCH(); 1967 } 1968 // Else slot is NULL. Fall through to slow path to raise AttributeError(name). 1969 } 1970 // Else it's a slot of a different type. We don't handle those. 1971 } 1972 // Else it's some other kind of descriptor that we don't handle. 1973 OPCACHE_DEOPT_LOAD_ATTR(); 1974 } 1975 else if (type->tp_dictoffset > 0) { 1976 // We found an instance with a __dict__. 1977 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset); 1978 dict = *dictptr; 1979 1980 if (dict != NULL && PyDict_CheckExact(dict)) { 1981 Py_INCREF(dict); 1982 res = NULL; 1983 assert(!_PyErr_Occurred(tstate)); 1984 Py_ssize_t hint = _PyDict_GetItemHint((PyDictObject*)dict, name, -1, &res); 1985 if (res != NULL) { 1986 Py_INCREF(res); 1987 Py_DECREF(dict); 1988 Py_DECREF(owner); 1989 SET_TOP(res); 1990 1991 if (co_opcache->optimized == 0) { 1992 // First time we optimize this opcode. 1993 OPCACHE_STAT_ATTR_OPT(); 1994 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES; 1995 } 1996 1997 la = &co_opcache->u.la; 1998 la->type = type; 1999 la->tp_version_tag = type->tp_version_tag; 2000 assert(hint >= 0); 2001 la->hint = hint; 2002 2003 DISPATCH(); 2004 } 2005 else { 2006 _PyErr_Clear(tstate); 2007 } 2008 Py_DECREF(dict); 2009 } else { 2010 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact. 2011 OPCACHE_DEOPT_LOAD_ATTR(); 2012 } 2013 } else { 2014 // The object's class does not have a tp_dictoffset we can use. 2015 OPCACHE_DEOPT_LOAD_ATTR(); 2016 } 2017 } else if (type->tp_getattro != PyObject_GenericGetAttr) { 2018 OPCACHE_DEOPT_LOAD_ATTR(); 2019 } 2020 } 2021 2022 // Slow path. 2023 res = PyObject_GetAttr(owner, name); 2024 Py_DECREF(owner); 2025 SET_TOP(res); 2026 if (res == NULL) 2027 goto error; 2028 DISPATCH(); 2029 } 2030 2031 case TARGET(COMPARE_OP): { 2032 assert(oparg <= Py_GE); 2033 PyObject *right = POP(); 2034 PyObject *left = TOP(); 2035 PyObject *res = PyObject_RichCompare(left, right, oparg); 2036 SET_TOP(res); 2037 Py_DECREF(left); 2038 Py_DECREF(right); 2039 if (res == NULL) 2040 goto error; 2041 PREDICT(POP_JUMP_IF_FALSE); 2042 PREDICT(POP_JUMP_IF_TRUE); 2043 DISPATCH(); 2044 } 2045 2046 case TARGET(IS_OP): { 2047 PyObject *right = POP(); 2048 PyObject *left = TOP(); 2049 int res = Py_Is(left, right) ^ oparg; 2050 PyObject *b = res ? Py_True : Py_False; 2051 Py_INCREF(b); 2052 SET_TOP(b); 2053 Py_DECREF(left); 2054 Py_DECREF(right); 2055 PREDICT(POP_JUMP_IF_FALSE); 2056 PREDICT(POP_JUMP_IF_TRUE); 2057 DISPATCH(); 2058 } 2059 2060 case TARGET(CONTAINS_OP): { 2061 PyObject *right = POP(); 2062 PyObject *left = POP(); 2063 int res = PySequence_Contains(right, left); 2064 Py_DECREF(left); 2065 Py_DECREF(right); 2066 if (res < 0) { 2067 goto error; 2068 } 2069 PyObject *b = (res^oparg) ? Py_True : Py_False; 2070 Py_INCREF(b); 2071 PUSH(b); 2072 PREDICT(POP_JUMP_IF_FALSE); 2073 PREDICT(POP_JUMP_IF_TRUE); 2074 DISPATCH(); 2075 } 2076 2077 #define CANNOT_CATCH_MSG "catching classes that do not inherit from "\ 2078 "BaseException is not allowed" 2079 2080 case TARGET(JUMP_IF_NOT_EXC_MATCH): { 2081 PyObject *right = POP(); 2082 PyObject *left = POP(); 2083 if (PyTuple_Check(right)) { 2084 Py_ssize_t i, length; 2085 length = PyTuple_GET_SIZE(right); 2086 for (i = 0; i < length; i++) { 2087 PyObject *exc = PyTuple_GET_ITEM(right, i); 2088 if (!PyExceptionClass_Check(exc)) { 2089 _PyErr_SetString(tstate, PyExc_TypeError, 2090 CANNOT_CATCH_MSG); 2091 Py_DECREF(left); 2092 Py_DECREF(right); 2093 goto error; 2094 } 2095 } 2096 } 2097 else { 2098 if (!PyExceptionClass_Check(right)) { 2099 _PyErr_SetString(tstate, PyExc_TypeError, 2100 CANNOT_CATCH_MSG); 2101 Py_DECREF(left); 2102 Py_DECREF(right); 2103 goto error; 2104 } 2105 } 2106 int res = PyErr_GivenExceptionMatches(left, right); 2107 Py_DECREF(left); 2108 Py_DECREF(right); 2109 if (res > 0) { 2110 /* Exception matches -- Do nothing */; 2111 } 2112 else if (res == 0) { 2113 JUMPTO(oparg); 2114 } 2115 else { 2116 goto error; 2117 } 2118 DISPATCH(); 2119 } 2120 2121 case TARGET(IMPORT_NAME): { 2122 PyObject *name = GETITEM(names, oparg); 2123 PyObject *fromlist = POP(); 2124 PyObject *level = TOP(); 2125 PyObject *res; 2126 res = import_name(tstate, f, name, fromlist, level); 2127 Py_DECREF(level); 2128 Py_DECREF(fromlist); 2129 SET_TOP(res); 2130 if (res == NULL) 2131 goto error; 2132 DISPATCH(); 2133 } 2134 2135 case TARGET(IMPORT_STAR): { 2136 PyObject *from = POP(), *locals; 2137 int err; 2138 if (PyFrame_FastToLocalsWithError(f) < 0) { 2139 Py_DECREF(from); 2140 goto error; 2141 } 2142 2143 locals = f->f_locals; 2144 if (locals == NULL) { 2145 _PyErr_SetString(tstate, PyExc_SystemError, 2146 "no locals found during 'import *'"); 2147 Py_DECREF(from); 2148 goto error; 2149 } 2150 err = import_all_from(tstate, locals, from); 2151 PyFrame_LocalsToFast(f, 0); 2152 Py_DECREF(from); 2153 if (err != 0) 2154 goto error; 2155 DISPATCH(); 2156 } 2157 2158 case TARGET(IMPORT_FROM): { 2159 PyObject *name = GETITEM(names, oparg); 2160 PyObject *from = TOP(); 2161 PyObject *res; 2162 res = import_from(tstate, from, name); 2163 PUSH(res); 2164 if (res == NULL) 2165 goto error; 2166 DISPATCH(); 2167 } 2168 2169 case TARGET(JUMP_FORWARD): { 2170 JUMPBY(oparg); 2171 DISPATCH(); 2172 } 2173 2174 case TARGET(POP_JUMP_IF_FALSE): { 2175 PREDICTED(POP_JUMP_IF_FALSE); 2176 PyObject *cond = POP(); 2177 int err; 2178 if (Py_IsTrue(cond)) { 2179 Py_DECREF(cond); 2180 DISPATCH(); 2181 } 2182 if (Py_IsFalse(cond)) { 2183 Py_DECREF(cond); 2184 JUMPTO(oparg); 2185 CHECK_EVAL_BREAKER(); 2186 DISPATCH(); 2187 } 2188 err = PyObject_IsTrue(cond); 2189 Py_DECREF(cond); 2190 if (err > 0) 2191 ; 2192 else if (err == 0) { 2193 JUMPTO(oparg); 2194 CHECK_EVAL_BREAKER(); 2195 } 2196 else 2197 goto error; 2198 DISPATCH(); 2199 } 2200 2201 case TARGET(POP_JUMP_IF_TRUE): { 2202 PREDICTED(POP_JUMP_IF_TRUE); 2203 PyObject *cond = POP(); 2204 int err; 2205 if (Py_IsFalse(cond)) { 2206 Py_DECREF(cond); 2207 DISPATCH(); 2208 } 2209 if (Py_IsTrue(cond)) { 2210 Py_DECREF(cond); 2211 JUMPTO(oparg); 2212 CHECK_EVAL_BREAKER(); 2213 DISPATCH(); 2214 } 2215 err = PyObject_IsTrue(cond); 2216 Py_DECREF(cond); 2217 if (err > 0) { 2218 JUMPTO(oparg); 2219 CHECK_EVAL_BREAKER(); 2220 } 2221 else if (err == 0) 2222 ; 2223 else 2224 goto error; 2225 DISPATCH(); 2226 } 2227 2228 case TARGET(JUMP_IF_FALSE_OR_POP): { 2229 PyObject *cond = TOP(); 2230 int err; 2231 if (Py_IsTrue(cond)) { 2232 STACK_SHRINK(1); 2233 Py_DECREF(cond); 2234 DISPATCH(); 2235 } 2236 if (Py_IsFalse(cond)) { 2237 JUMPTO(oparg); 2238 DISPATCH(); 2239 } 2240 err = PyObject_IsTrue(cond); 2241 if (err > 0) { 2242 STACK_SHRINK(1); 2243 Py_DECREF(cond); 2244 } 2245 else if (err == 0) 2246 JUMPTO(oparg); 2247 else 2248 goto error; 2249 DISPATCH(); 2250 } 2251 2252 case TARGET(JUMP_IF_TRUE_OR_POP): { 2253 PyObject *cond = TOP(); 2254 int err; 2255 if (Py_IsFalse(cond)) { 2256 STACK_SHRINK(1); 2257 Py_DECREF(cond); 2258 DISPATCH(); 2259 } 2260 if (Py_IsTrue(cond)) { 2261 JUMPTO(oparg); 2262 DISPATCH(); 2263 } 2264 err = PyObject_IsTrue(cond); 2265 if (err > 0) { 2266 JUMPTO(oparg); 2267 } 2268 else if (err == 0) { 2269 STACK_SHRINK(1); 2270 Py_DECREF(cond); 2271 } 2272 else 2273 goto error; 2274 DISPATCH(); 2275 } 2276 2277 case TARGET(JUMP_ABSOLUTE): { 2278 PREDICTED(JUMP_ABSOLUTE); 2279 JUMPTO(oparg); 2280 CHECK_EVAL_BREAKER(); 2281 DISPATCH(); 2282 } 2283 2284 case TARGET(GET_LEN): { 2285 // PUSH(len(TOS)) 2286 Py_ssize_t len_i = PyObject_Length(TOP()); 2287 if (len_i < 0) { 2288 goto error; 2289 } 2290 PyObject *len_o = PyLong_FromSsize_t(len_i); 2291 if (len_o == NULL) { 2292 goto error; 2293 } 2294 PUSH(len_o); 2295 DISPATCH(); 2296 } 2297 2298 case TARGET(MATCH_CLASS): { 2299 // Pop TOS. On success, set TOS to True and TOS1 to a tuple of 2300 // attributes. On failure, set TOS to False. 2301 PyObject *names = POP(); 2302 PyObject *type = TOP(); 2303 PyObject *subject = SECOND(); 2304 assert(PyTuple_CheckExact(names)); 2305 PyObject *attrs = match_class(tstate, subject, type, oparg, names); 2306 Py_DECREF(names); 2307 if (attrs) { 2308 // Success! 2309 assert(PyTuple_CheckExact(attrs)); 2310 Py_DECREF(subject); 2311 SET_SECOND(attrs); 2312 } 2313 else if (_PyErr_Occurred(tstate)) { 2314 goto error; 2315 } 2316 Py_DECREF(type); 2317 SET_TOP(PyBool_FromLong(!!attrs)); 2318 DISPATCH(); 2319 } 2320 2321 case TARGET(MATCH_MAPPING): { 2322 PyObject *subject = TOP(); 2323 int match = Py_TYPE(subject)->tp_flags & Py_TPFLAGS_MAPPING; 2324 PyObject *res = match ? Py_True : Py_False; 2325 Py_INCREF(res); 2326 PUSH(res); 2327 DISPATCH(); 2328 } 2329 2330 case TARGET(MATCH_SEQUENCE): { 2331 PyObject *subject = TOP(); 2332 int match = Py_TYPE(subject)->tp_flags & Py_TPFLAGS_SEQUENCE; 2333 PyObject *res = match ? Py_True : Py_False; 2334 Py_INCREF(res); 2335 PUSH(res); 2336 DISPATCH(); 2337 } 2338 2339 case TARGET(MATCH_KEYS): { 2340 // On successful match for all keys, PUSH(values) and PUSH(True). 2341 // Otherwise, PUSH(None) and PUSH(False). 2342 PyObject *keys = TOP(); 2343 PyObject *subject = SECOND(); 2344 PyObject *values_or_none = match_keys(tstate, subject, keys); 2345 if (values_or_none == NULL) { 2346 goto error; 2347 } 2348 PUSH(values_or_none); 2349 if (Py_IsNone(values_or_none)) { 2350 Py_INCREF(Py_False); 2351 PUSH(Py_False); 2352 DISPATCH(); 2353 } 2354 assert(PyTuple_CheckExact(values_or_none)); 2355 Py_INCREF(Py_True); 2356 PUSH(Py_True); 2357 DISPATCH(); 2358 } 2359 2360 case TARGET(COPY_DICT_WITHOUT_KEYS): { 2361 // rest = dict(TOS1) 2362 // for key in TOS: 2363 // del rest[key] 2364 // SET_TOP(rest) 2365 PyObject *keys = TOP(); 2366 PyObject *subject = SECOND(); 2367 PyObject *rest = PyDict_New(); 2368 if (rest == NULL || PyDict_Update(rest, subject)) { 2369 Py_XDECREF(rest); 2370 goto error; 2371 } 2372 // This may seem a bit inefficient, but keys is rarely big enough to 2373 // actually impact runtime. 2374 assert(PyTuple_CheckExact(keys)); 2375 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(keys); i++) { 2376 if (PyDict_DelItem(rest, PyTuple_GET_ITEM(keys, i))) { 2377 Py_DECREF(rest); 2378 goto error; 2379 } 2380 } 2381 Py_DECREF(keys); 2382 SET_TOP(rest); 2383 DISPATCH(); 2384 } 2385 2386 case TARGET(GET_ITER): { 2387 /* before: [obj]; after [getiter(obj)] */ 2388 PyObject *iterable = TOP(); 2389 PyObject *iter = PyObject_GetIter(iterable); 2390 Py_DECREF(iterable); 2391 SET_TOP(iter); 2392 if (iter == NULL) 2393 goto error; 2394 PREDICT(FOR_ITER); 2395 PREDICT(CALL_FUNCTION); 2396 DISPATCH(); 2397 } 2398 2399 case TARGET(GET_YIELD_FROM_ITER): { 2400 /* before: [obj]; after [getiter(obj)] */ 2401 PyObject *iterable = TOP(); 2402 PyObject *iter; 2403 if (PyCoro_CheckExact(iterable)) { 2404 /* `iterable` is a coroutine */ 2405 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) { 2406 /* and it is used in a 'yield from' expression of a 2407 regular generator. */ 2408 Py_DECREF(iterable); 2409 SET_TOP(NULL); 2410 _PyErr_SetString(tstate, PyExc_TypeError, 2411 "cannot 'yield from' a coroutine object " 2412 "in a non-coroutine generator"); 2413 goto error; 2414 } 2415 } 2416 else if (!PyGen_CheckExact(iterable)) { 2417 /* `iterable` is not a generator. */ 2418 iter = PyObject_GetIter(iterable); 2419 Py_DECREF(iterable); 2420 SET_TOP(iter); 2421 if (iter == NULL) 2422 goto error; 2423 } 2424 PREDICT(LOAD_CONST); 2425 DISPATCH(); 2426 } 2427 2428 case TARGET(FOR_ITER): { 2429 PREDICTED(FOR_ITER); 2430 /* before: [iter]; after: [iter, iter()] *or* [] */ 2431 PyObject *iter = TOP(); 2432 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter); 2433 if (next != NULL) { 2434 PUSH(next); 2435 PREDICT(STORE_FAST); 2436 PREDICT(UNPACK_SEQUENCE); 2437 DISPATCH(); 2438 } 2439 if (_PyErr_Occurred(tstate)) { 2440 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) { 2441 goto error; 2442 } 2443 else if (tstate->c_tracefunc != NULL) { 2444 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &trace_info); 2445 } 2446 _PyErr_Clear(tstate); 2447 } 2448 /* iterator ended normally */ 2449 STACK_SHRINK(1); 2450 Py_DECREF(iter); 2451 JUMPBY(oparg); 2452 DISPATCH(); 2453 } 2454 2455 case TARGET(SETUP_FINALLY): { 2456 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, 2457 STACK_LEVEL()); 2458 DISPATCH(); 2459 } 2460 2461 case TARGET(BEFORE_ASYNC_WITH): { 2462 _Py_IDENTIFIER(__aenter__); 2463 _Py_IDENTIFIER(__aexit__); 2464 PyObject *mgr = TOP(); 2465 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__); 2466 PyObject *res; 2467 if (enter == NULL) { 2468 goto error; 2469 } 2470 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__); 2471 if (exit == NULL) { 2472 Py_DECREF(enter); 2473 goto error; 2474 } 2475 SET_TOP(exit); 2476 Py_DECREF(mgr); 2477 res = _PyObject_CallNoArg(enter); 2478 Py_DECREF(enter); 2479 if (res == NULL) 2480 goto error; 2481 PUSH(res); 2482 PREDICT(GET_AWAITABLE); 2483 DISPATCH(); 2484 } 2485 2486 case TARGET(SETUP_ASYNC_WITH): { 2487 PyObject *res = POP(); 2488 /* Setup the finally block before pushing the result 2489 of __aenter__ on the stack. */ 2490 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, 2491 STACK_LEVEL()); 2492 PUSH(res); 2493 DISPATCH(); 2494 } 2495 2496 case TARGET(SETUP_WITH): { 2497 _Py_IDENTIFIER(__enter__); 2498 _Py_IDENTIFIER(__exit__); 2499 PyObject *mgr = TOP(); 2500 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__); 2501 PyObject *res; 2502 if (enter == NULL) { 2503 goto error; 2504 } 2505 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__); 2506 if (exit == NULL) { 2507 Py_DECREF(enter); 2508 goto error; 2509 } 2510 SET_TOP(exit); 2511 Py_DECREF(mgr); 2512 res = _PyObject_CallNoArg(enter); 2513 Py_DECREF(enter); 2514 if (res == NULL) 2515 goto error; 2516 /* Setup the finally block before pushing the result 2517 of __enter__ on the stack. */ 2518 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg, 2519 STACK_LEVEL()); 2520 2521 PUSH(res); 2522 DISPATCH(); 2523 } 2524 2525 case TARGET(WITH_EXCEPT_START): { 2526 /* At the top of the stack are 7 values: 2527 - (TOP, SECOND, THIRD) = exc_info() 2528 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER 2529 - SEVENTH: the context.__exit__ bound method 2530 We call SEVENTH(TOP, SECOND, THIRD). 2531 Then we push again the TOP exception and the __exit__ 2532 return value. 2533 */ 2534 PyObject *exit_func; 2535 PyObject *exc, *val, *tb, *res; 2536 2537 exc = TOP(); 2538 val = SECOND(); 2539 tb = THIRD(); 2540 assert(!Py_IsNone(exc)); 2541 assert(!PyLong_Check(exc)); 2542 exit_func = PEEK(7); 2543 PyObject *stack[4] = {NULL, exc, val, tb}; 2544 res = PyObject_Vectorcall(exit_func, stack + 1, 2545 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL); 2546 if (res == NULL) 2547 goto error; 2548 2549 PUSH(res); 2550 DISPATCH(); 2551 } 2552 2553 case TARGET(LOAD_METHOD): { 2554 /* Designed to work in tandem with CALL_METHOD. */ 2555 PyObject *name = GETITEM(names, oparg); 2556 PyObject *obj = TOP(); 2557 PyObject *meth = NULL; 2558 2559 int meth_found = _PyObject_GetMethod(obj, name, &meth); 2560 2561 if (meth == NULL) { 2562 /* Most likely attribute wasn't found. */ 2563 goto error; 2564 } 2565 2566 if (meth_found) { 2567 /* We can bypass temporary bound method object. 2568 meth is unbound method and obj is self. 2569 2570 meth | self | arg1 | ... | argN 2571 */ 2572 SET_TOP(meth); 2573 PUSH(obj); // self 2574 } 2575 else { 2576 /* meth is not an unbound method (but a regular attr, or 2577 something was returned by a descriptor protocol). Set 2578 the second element of the stack to NULL, to signal 2579 CALL_METHOD that it's not a method call. 2580 2581 NULL | meth | arg1 | ... | argN 2582 */ 2583 SET_TOP(NULL); 2584 Py_DECREF(obj); 2585 PUSH(meth); 2586 } 2587 DISPATCH(); 2588 } 2589 2590 case TARGET(CALL_METHOD): { 2591 /* Designed to work in tamdem with LOAD_METHOD. */ 2592 PyObject **sp, *res, *meth; 2593 2594 sp = stack_pointer; 2595 2596 meth = PEEK(oparg + 2); 2597 if (meth == NULL) { 2598 /* `meth` is NULL when LOAD_METHOD thinks that it's not 2599 a method call. 2600 2601 Stack layout: 2602 2603 ... | NULL | callable | arg1 | ... | argN 2604 ^- TOP() 2605 ^- (-oparg) 2606 ^- (-oparg-1) 2607 ^- (-oparg-2) 2608 2609 `callable` will be POPed by call_function. 2610 NULL will will be POPed manually later. 2611 */ 2612 res = call_function(tstate, &trace_info, &sp, oparg, NULL); 2613 stack_pointer = sp; 2614 (void)POP(); /* POP the NULL. */ 2615 } 2616 else { 2617 /* This is a method call. Stack layout: 2618 2619 ... | method | self | arg1 | ... | argN 2620 ^- TOP() 2621 ^- (-oparg) 2622 ^- (-oparg-1) 2623 ^- (-oparg-2) 2624 2625 `self` and `method` will be POPed by call_function. 2626 We'll be passing `oparg + 1` to call_function, to 2627 make it accept the `self` as a first argument. 2628 */ 2629 res = call_function(tstate, &trace_info, &sp, oparg + 1, NULL); 2630 stack_pointer = sp; 2631 } 2632 2633 PUSH(res); 2634 if (res == NULL) 2635 goto error; 2636 CHECK_EVAL_BREAKER(); 2637 DISPATCH(); 2638 } 2639 2640 case TARGET(CALL_FUNCTION): { 2641 PREDICTED(CALL_FUNCTION); 2642 PyObject **sp, *res; 2643 sp = stack_pointer; 2644 res = call_function(tstate, &trace_info, &sp, oparg, NULL); 2645 stack_pointer = sp; 2646 PUSH(res); 2647 if (res == NULL) { 2648 goto error; 2649 } 2650 CHECK_EVAL_BREAKER(); 2651 DISPATCH(); 2652 } 2653 2654 case TARGET(CALL_FUNCTION_KW): { 2655 PyObject **sp, *res, *names; 2656 2657 names = POP(); 2658 assert(PyTuple_Check(names)); 2659 assert(PyTuple_GET_SIZE(names) <= oparg); 2660 /* We assume without checking that names contains only strings */ 2661 sp = stack_pointer; 2662 res = call_function(tstate, &trace_info, &sp, oparg, names); 2663 stack_pointer = sp; 2664 PUSH(res); 2665 Py_DECREF(names); 2666 2667 if (res == NULL) { 2668 goto error; 2669 } 2670 CHECK_EVAL_BREAKER(); 2671 DISPATCH(); 2672 } 2673 2674 case TARGET(CALL_FUNCTION_EX): { 2675 PREDICTED(CALL_FUNCTION_EX); 2676 PyObject *func, *callargs, *kwargs = NULL, *result; 2677 if (oparg & 0x01) { 2678 kwargs = POP(); 2679 if (!PyDict_CheckExact(kwargs)) { 2680 PyObject *d = PyDict_New(); 2681 if (d == NULL) 2682 goto error; 2683 if (_PyDict_MergeEx(d, kwargs, 2) < 0) { 2684 Py_DECREF(d); 2685 format_kwargs_error(tstate, SECOND(), kwargs); 2686 Py_DECREF(kwargs); 2687 goto error; 2688 } 2689 Py_DECREF(kwargs); 2690 kwargs = d; 2691 } 2692 assert(PyDict_CheckExact(kwargs)); 2693 } 2694 callargs = POP(); 2695 func = TOP(); 2696 if (!PyTuple_CheckExact(callargs)) { 2697 if (check_args_iterable(tstate, func, callargs) < 0) { 2698 Py_DECREF(callargs); 2699 goto error; 2700 } 2701 Py_SETREF(callargs, PySequence_Tuple(callargs)); 2702 if (callargs == NULL) { 2703 goto error; 2704 } 2705 } 2706 assert(PyTuple_CheckExact(callargs)); 2707 2708 result = do_call_core(tstate, &trace_info, func, callargs, kwargs); 2709 Py_DECREF(func); 2710 Py_DECREF(callargs); 2711 Py_XDECREF(kwargs); 2712 2713 SET_TOP(result); 2714 if (result == NULL) { 2715 goto error; 2716 } 2717 CHECK_EVAL_BREAKER(); 2718 DISPATCH(); 2719 } 2720 2721 case TARGET(MAKE_FUNCTION): { 2722 PyObject *qualname = POP(); 2723 PyObject *codeobj = POP(); 2724 PyFunctionObject *func = (PyFunctionObject *) 2725 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname); 2726 2727 Py_DECREF(codeobj); 2728 Py_DECREF(qualname); 2729 if (func == NULL) { 2730 goto error; 2731 } 2732 2733 if (oparg & 0x08) { 2734 assert(PyTuple_CheckExact(TOP())); 2735 func->func_closure = POP(); 2736 } 2737 if (oparg & 0x04) { 2738 assert(PyTuple_CheckExact(TOP())); 2739 func->func_annotations = POP(); 2740 } 2741 if (oparg & 0x02) { 2742 assert(PyDict_CheckExact(TOP())); 2743 func->func_kwdefaults = POP(); 2744 } 2745 if (oparg & 0x01) { 2746 assert(PyTuple_CheckExact(TOP())); 2747 func->func_defaults = POP(); 2748 } 2749 2750 PUSH((PyObject *)func); 2751 DISPATCH(); 2752 } 2753 2754 case TARGET(BUILD_SLICE): { 2755 PyObject *start, *stop, *step, *slice; 2756 if (oparg == 3) 2757 step = POP(); 2758 else 2759 step = NULL; 2760 stop = POP(); 2761 start = TOP(); 2762 slice = PySlice_New(start, stop, step); 2763 Py_DECREF(start); 2764 Py_DECREF(stop); 2765 Py_XDECREF(step); 2766 SET_TOP(slice); 2767 if (slice == NULL) 2768 goto error; 2769 DISPATCH(); 2770 } 2771 2772 case TARGET(FORMAT_VALUE): { 2773 /* Handles f-string value formatting. */ 2774 PyObject *result; 2775 PyObject *fmt_spec; 2776 PyObject *value; 2777 PyObject *(*conv_fn)(PyObject *); 2778 int which_conversion = oparg & FVC_MASK; 2779 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC; 2780 2781 fmt_spec = have_fmt_spec ? POP() : NULL; 2782 value = POP(); 2783 2784 /* See if any conversion is specified. */ 2785 switch (which_conversion) { 2786 case FVC_NONE: conv_fn = NULL; break; 2787 case FVC_STR: conv_fn = PyObject_Str; break; 2788 case FVC_REPR: conv_fn = PyObject_Repr; break; 2789 case FVC_ASCII: conv_fn = PyObject_ASCII; break; 2790 default: 2791 _PyErr_Format(tstate, PyExc_SystemError, 2792 "unexpected conversion flag %d", 2793 which_conversion); 2794 goto error; 2795 } 2796 2797 /* If there's a conversion function, call it and replace 2798 value with that result. Otherwise, just use value, 2799 without conversion. */ 2800 if (conv_fn != NULL) { 2801 result = conv_fn(value); 2802 Py_DECREF(value); 2803 if (result == NULL) { 2804 Py_XDECREF(fmt_spec); 2805 goto error; 2806 } 2807 value = result; 2808 } 2809 2810 /* If value is a unicode object, and there's no fmt_spec, 2811 then we know the result of format(value) is value 2812 itself. In that case, skip calling format(). I plan to 2813 move this optimization in to PyObject_Format() 2814 itself. */ 2815 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) { 2816 /* Do nothing, just transfer ownership to result. */ 2817 result = value; 2818 } else { 2819 /* Actually call format(). */ 2820 result = PyObject_Format(value, fmt_spec); 2821 Py_DECREF(value); 2822 Py_XDECREF(fmt_spec); 2823 if (result == NULL) { 2824 goto error; 2825 } 2826 } 2827 2828 PUSH(result); 2829 DISPATCH(); 2830 } 2831 2832 case TARGET(ROT_N): { 2833 PyObject *top = TOP(); 2834 memmove(&PEEK(oparg - 1), &PEEK(oparg), 2835 sizeof(PyObject*) * (oparg - 1)); 2836 PEEK(oparg) = top; 2837 DISPATCH(); 2838 } 2839 2840 case TARGET(EXTENDED_ARG): { 2841 int oldoparg = oparg; 2842 NEXTOPARG(); 2843 oparg |= oldoparg << 8; 2844 goto dispatch_opcode; 2845 } 2846 2847 2848 #if USE_COMPUTED_GOTOS 2849 _unknown_opcode: 2850 #endif 2851 default: 2852 fprintf(stderr, 2853 "XXX lineno: %d, opcode: %d\n", 2854 PyFrame_GetLineNumber(f), 2855 opcode); 2856 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode"); 2857 goto error; 2858 2859 } /* switch */ 2860 2861 /* This should never be reached. Every opcode should end with DISPATCH() 2862 or goto error. */ 2863 Py_UNREACHABLE(); 2864 2865 error: 2866 /* Double-check exception status. */ 2867 #ifdef NDEBUG 2868 if (!_PyErr_Occurred(tstate)) { 2869 _PyErr_SetString(tstate, PyExc_SystemError, 2870 "error return without exception set"); 2871 } 2872 #else 2873 assert(_PyErr_Occurred(tstate)); 2874 #endif 2875 2876 /* Log traceback info. */ 2877 PyTraceBack_Here(f); 2878 2879 if (tstate->c_tracefunc != NULL) { 2880 /* Make sure state is set to FRAME_EXECUTING for tracing */ 2881 assert(f->f_state == FRAME_EXECUTING); 2882 f->f_state = FRAME_UNWINDING; 2883 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, 2884 tstate, f, &trace_info); 2885 } 2886 exception_unwind: 2887 f->f_state = FRAME_UNWINDING; 2888 /* Unwind stacks if an exception occurred */ 2889 while (f->f_iblock > 0) { 2890 /* Pop the current block. */ 2891 PyTryBlock *b = &f->f_blockstack[--f->f_iblock]; 2892 2893 if (b->b_type == EXCEPT_HANDLER) { 2894 UNWIND_EXCEPT_HANDLER(b); 2895 continue; 2896 } 2897 UNWIND_BLOCK(b); 2898 if (b->b_type == SETUP_FINALLY) { 2899 PyObject *exc, *val, *tb; 2900 int handler = b->b_handler; 2901 _PyErr_StackItem *exc_info = tstate->exc_info; 2902 /* Beware, this invalidates all b->b_* fields */ 2903 PyFrame_BlockSetup(f, EXCEPT_HANDLER, f->f_lasti, STACK_LEVEL()); 2904 PUSH(exc_info->exc_traceback); 2905 PUSH(exc_info->exc_value); 2906 if (exc_info->exc_type != NULL) { 2907 PUSH(exc_info->exc_type); 2908 } 2909 else { 2910 Py_INCREF(Py_None); 2911 PUSH(Py_None); 2912 } 2913 _PyErr_Fetch(tstate, &exc, &val, &tb); 2914 /* Make the raw exception data 2915 available to the handler, 2916 so a program can emulate the 2917 Python main loop. */ 2918 _PyErr_NormalizeException(tstate, &exc, &val, &tb); 2919 if (tb != NULL) 2920 PyException_SetTraceback(val, tb); 2921 else 2922 PyException_SetTraceback(val, Py_None); 2923 Py_INCREF(exc); 2924 exc_info->exc_type = exc; 2925 Py_INCREF(val); 2926 exc_info->exc_value = val; 2927 exc_info->exc_traceback = tb; 2928 if (tb == NULL) 2929 tb = Py_None; 2930 Py_INCREF(tb); 2931 PUSH(tb); 2932 PUSH(val); 2933 PUSH(exc); 2934 JUMPTO(handler); 2935 /* Resume normal execution */ 2936 f->f_state = FRAME_EXECUTING; 2937 goto main_loop; 2938 } 2939 } /* unwind stack */ 2940 2941 /* End the loop as we still have an error */ 2942 break; 2943 } /* main loop */ 2944 2945 assert(retval == NULL); 2946 assert(_PyErr_Occurred(tstate)); 2947 2948 /* Pop remaining stack entries. */ 2949 while (!EMPTY()) { 2950 PyObject *o = POP(); 2951 Py_XDECREF(o); 2952 } 2953 f->f_stackdepth = 0; 2954 f->f_state = FRAME_RAISED; 2955 exiting: 2956 if (trace_info.cframe.use_tracing) { 2957 if (tstate->c_tracefunc) { 2958 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj, 2959 tstate, f, &trace_info, PyTrace_RETURN, retval)) { 2960 Py_CLEAR(retval); 2961 } 2962 } 2963 if (tstate->c_profilefunc) { 2964 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj, 2965 tstate, f, &trace_info, PyTrace_RETURN, retval)) { 2966 Py_CLEAR(retval); 2967 } 2968 } 2969 } 2970 2971 /* pop frame */ 2972 exit_eval_frame: 2973 /* Restore previous cframe */ 2974 tstate->cframe = trace_info.cframe.previous; 2975 tstate->cframe->use_tracing = trace_info.cframe.use_tracing; 2976 2977 if (PyDTrace_FUNCTION_RETURN_ENABLED()) 2978 dtrace_function_return(f); 2979 _Py_LeaveRecursiveCall(tstate); 2980 tstate->frame = f->f_back; 2981 2982 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__); 2983 } 2984 2985 static PyObject * 2986 make_coro(PyFrameConstructor *con, PyFrameObject *f) 2987 { 2988 assert (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)); 2989 PyObject *gen; 2990 int is_coro = ((PyCodeObject *)con->fc_code)->co_flags & CO_COROUTINE; 2991 2992 /* Don't need to keep the reference to f_back, it will be set 2993 * when the generator is resumed. */ 2994 Py_CLEAR(f->f_back); 2995 2996 /* Create a new generator that owns the ready to run frame 2997 * and return that as the value. */ 2998 if (is_coro) { 2999 gen = PyCoro_New(f, con->fc_name, con->fc_qualname); 3000 } else if (((PyCodeObject *)con->fc_code)->co_flags & CO_ASYNC_GENERATOR) { 3001 gen = PyAsyncGen_New(f, con->fc_name, con->fc_qualname); 3002 } else { 3003 gen = PyGen_NewWithQualName(f, con->fc_name, con->fc_qualname); 3004 } 3005 if (gen == NULL) { 3006 return NULL; 3007 } 3008 3009 _PyObject_GC_TRACK(f); 3010 3011 return gen; 3012 } 3013 3014 PyObject * 3015 _PyEval_Vector(PyThreadState *tstate, PyFrameConstructor *con, 3016 PyObject *locals, 3017 PyObject* const* args, size_t argcount, 3018 PyObject *kwnames) 3019 { 3020 PyFrameObject *f = _PyEval_MakeFrameVector( 3021 tstate, con, locals, args, argcount, kwnames); 3022 if (f == NULL) { 3023 return NULL; 3024 } 3025 if (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) { 3026 return make_coro(con, f); 3027 } 3028 PyObject *retval = _PyEval_EvalFrame(tstate, f, 0); 3029 3030 /* decref'ing the frame can cause __del__ methods to get invoked, 3031 which can call back into Python. While we're done with the 3032 current Python frame (f), the associated C stack is still in use, 3033 so recursion_depth must be boosted for the duration. 3034 */ 3035 if (Py_REFCNT(f) > 1) { 3036 Py_DECREF(f); 3037 _PyObject_GC_TRACK(f); 3038 } 3039 else { 3040 ++tstate->recursion_depth; 3041 Py_DECREF(f); 3042 --tstate->recursion_depth; 3043 } 3044 return retval; 3045 }