LCOV - code coverage report
Current view: top level - Objects - frameobject.c (source / functions) Hit Total Coverage
Test: CPython lcov report Lines: 543 632 85.9 %
Date: 2022-07-07 18:19:46 Functions: 46 48 95.8 %

          Line data    Source code
       1             : /* Frame object implementation */
       2             : 
       3             : #include "Python.h"
       4             : #include "pycore_ceval.h"         // _PyEval_BuiltinsFromGlobals()
       5             : #include "pycore_code.h"          // CO_FAST_LOCAL, etc.
       6             : #include "pycore_function.h"      // _PyFunction_FromConstructor()
       7             : #include "pycore_moduleobject.h"  // _PyModule_GetDict()
       8             : #include "pycore_object.h"        // _PyObject_GC_UNTRACK()
       9             : #include "pycore_opcode.h"        // _PyOpcode_Caches
      10             : 
      11             : #include "frameobject.h"          // PyFrameObject
      12             : #include "pycore_frame.h"
      13             : #include "opcode.h"               // EXTENDED_ARG
      14             : #include "structmember.h"         // PyMemberDef
      15             : 
      16             : #define OFF(x) offsetof(PyFrameObject, x)
      17             : 
      18             : static PyMemberDef frame_memberlist[] = {
      19             :     {"f_trace_lines",   T_BOOL,         OFF(f_trace_lines), 0},
      20             :     {"f_trace_opcodes", T_BOOL,         OFF(f_trace_opcodes), 0},
      21             :     {NULL}      /* Sentinel */
      22             : };
      23             : 
      24             : 
      25             : static PyObject *
      26         537 : frame_getlocals(PyFrameObject *f, void *closure)
      27             : {
      28         537 :     if (PyFrame_FastToLocalsWithError(f) < 0)
      29           0 :         return NULL;
      30         537 :     PyObject *locals = f->f_frame->f_locals;
      31         537 :     Py_INCREF(locals);
      32         537 :     return locals;
      33             : }
      34             : 
      35             : int
      36     7067210 : PyFrame_GetLineNumber(PyFrameObject *f)
      37             : {
      38     7067210 :     assert(f != NULL);
      39     7067210 :     if (f->f_lineno != 0) {
      40      589133 :         return f->f_lineno;
      41             :     }
      42             :     else {
      43     6478080 :         return _PyInterpreterFrame_GetLine(f->f_frame);
      44             :     }
      45             : }
      46             : 
      47             : static PyObject *
      48      718951 : frame_getlineno(PyFrameObject *f, void *closure)
      49             : {
      50      718951 :     int lineno = PyFrame_GetLineNumber(f);
      51      718951 :     if (lineno < 0) {
      52         103 :         Py_RETURN_NONE;
      53             :     }
      54             :     else {
      55      718848 :         return PyLong_FromLong(lineno);
      56             :     }
      57             : }
      58             : 
      59             : static PyObject *
      60         582 : frame_getlasti(PyFrameObject *f, void *closure)
      61             : {
      62         582 :     int lasti = _PyInterpreterFrame_LASTI(f->f_frame);
      63         582 :     if (lasti < 0) {
      64           0 :         return PyLong_FromLong(-1);
      65             :     }
      66         582 :     return PyLong_FromLong(lasti * sizeof(_Py_CODEUNIT));
      67             : }
      68             : 
      69             : static PyObject *
      70      282968 : frame_getglobals(PyFrameObject *f, void *closure)
      71             : {
      72      282968 :     PyObject *globals = f->f_frame->f_globals;
      73      282968 :     if (globals == NULL) {
      74           0 :         globals = Py_None;
      75             :     }
      76      282968 :     Py_INCREF(globals);
      77      282968 :     return globals;
      78             : }
      79             : 
      80             : static PyObject *
      81           3 : frame_getbuiltins(PyFrameObject *f, void *closure)
      82             : {
      83           3 :     PyObject *builtins = f->f_frame->f_builtins;
      84           3 :     if (builtins == NULL) {
      85           0 :         builtins = Py_None;
      86             :     }
      87           3 :     Py_INCREF(builtins);
      88           3 :     return builtins;
      89             : }
      90             : 
      91             : static PyObject *
      92      873498 : frame_getcode(PyFrameObject *f, void *closure)
      93             : {
      94      873498 :     if (PySys_Audit("object.__getattr__", "Os", f, "f_code") < 0) {
      95           0 :         return NULL;
      96             :     }
      97      873498 :     return (PyObject *)PyFrame_GetCode(f);
      98             : }
      99             : 
     100             : static PyObject *
     101      140733 : frame_getback(PyFrameObject *f, void *closure)
     102             : {
     103      140733 :     PyObject *res = (PyObject *)PyFrame_GetBack(f);
     104      140733 :     if (res == NULL) {
     105        2456 :         Py_RETURN_NONE;
     106             :     }
     107      138277 :     return res;
     108             : }
     109             : 
     110             : // Given the index of the effective opcode, scan back to construct the oparg
     111             : // with EXTENDED_ARG. This only works correctly with *unquickened* code,
     112             : // obtained via a call to _PyCode_GetCode!
     113             : static unsigned int
     114         402 : get_arg(const _Py_CODEUNIT *codestr, Py_ssize_t i)
     115             : {
     116             :     _Py_CODEUNIT word;
     117         402 :     unsigned int oparg = _Py_OPARG(codestr[i]);
     118         402 :     if (i >= 1 && _Py_OPCODE(word = codestr[i-1]) == EXTENDED_ARG) {
     119           0 :         oparg |= _Py_OPARG(word) << 8;
     120           0 :         if (i >= 2 && _Py_OPCODE(word = codestr[i-2]) == EXTENDED_ARG) {
     121           0 :             oparg |= _Py_OPARG(word) << 16;
     122           0 :             if (i >= 3 && _Py_OPCODE(word = codestr[i-3]) == EXTENDED_ARG) {
     123           0 :                 oparg |= _Py_OPARG(word) << 24;
     124             :             }
     125             :         }
     126             :     }
     127         402 :     return oparg;
     128             : }
     129             : 
     130             : /* Model the evaluation stack, to determine which jumps
     131             :  * are safe and how many values needs to be popped.
     132             :  * The stack is modelled by a 64 integer, treating any
     133             :  * stack that can't fit into 64 bits as "overflowed".
     134             :  */
     135             : 
     136             : typedef enum kind {
     137             :     Iterator = 1,
     138             :     Except = 2,
     139             :     Object = 3,
     140             :     Null = 4,
     141             : } Kind;
     142             : 
     143             : static int
     144          11 : compatible_kind(Kind from, Kind to) {
     145          11 :     if (to == 0) {
     146           0 :         return 0;
     147             :     }
     148          11 :     if (to == Object) {
     149           3 :         return from != Null;
     150             :     }
     151           8 :     if (to == Null) {
     152           4 :         return 1;
     153             :     }
     154           4 :     return from == to;
     155             : }
     156             : 
     157             : #define BITS_PER_BLOCK 3
     158             : 
     159             : #define UNINITIALIZED -2
     160             : #define OVERFLOWED -1
     161             : 
     162             : #define MAX_STACK_ENTRIES (63/BITS_PER_BLOCK)
     163             : #define WILL_OVERFLOW (1ULL<<((MAX_STACK_ENTRIES-1)*BITS_PER_BLOCK))
     164             : 
     165             : static inline int64_t
     166        1499 : push_value(int64_t stack, Kind kind)
     167             : {
     168        1499 :     if (((uint64_t)stack) >= WILL_OVERFLOW) {
     169           0 :         return OVERFLOWED;
     170             :     }
     171             :     else {
     172        1499 :         return (stack << BITS_PER_BLOCK) | kind;
     173             :     }
     174             : }
     175             : 
     176             : static inline int64_t
     177        1476 : pop_value(int64_t stack)
     178             : {
     179        1476 :     return Py_ARITHMETIC_RIGHT_SHIFT(int64_t, stack, BITS_PER_BLOCK);
     180             : }
     181             : 
     182             : static inline Kind
     183          34 : top_of_stack(int64_t stack)
     184             : {
     185          34 :     return stack & ((1<<BITS_PER_BLOCK)-1);
     186             : }
     187             : 
     188             : static int64_t *
     189          88 : mark_stacks(PyCodeObject *code_obj, int len)
     190             : {
     191          88 :     PyObject *co_code = _PyCode_GetCode(code_obj);
     192          88 :     if (co_code == NULL) {
     193           0 :         return NULL;
     194             :     }
     195          88 :     _Py_CODEUNIT *code = (_Py_CODEUNIT *)PyBytes_AS_STRING(co_code);
     196          88 :     int64_t *stacks = PyMem_New(int64_t, len+1);
     197             :     int i, j, opcode;
     198             : 
     199          88 :     if (stacks == NULL) {
     200           0 :         PyErr_NoMemory();
     201           0 :         Py_DECREF(co_code);
     202           0 :         return NULL;
     203             :     }
     204        8057 :     for (int i = 1; i <= len; i++) {
     205        7969 :         stacks[i] = UNINITIALIZED;
     206             :     }
     207          88 :     stacks[0] = 0;
     208          88 :     if (code_obj->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR))
     209             :     {
     210             :         // Generators get sent None while starting:
     211          20 :         stacks[0] = push_value(stacks[0], Object);
     212             :     }
     213          88 :     int todo = 1;
     214         176 :     while (todo) {
     215          88 :         todo = 0;
     216        8057 :         for (i = 0; i < len; i++) {
     217        7969 :             int64_t next_stack = stacks[i];
     218        7969 :             if (next_stack == UNINITIALIZED) {
     219        1975 :                 continue;
     220             :             }
     221        5994 :             opcode = _Py_OPCODE(code[i]);
     222        5994 :             switch (opcode) {
     223          12 :                 case JUMP_IF_FALSE_OR_POP:
     224             :                 case JUMP_IF_TRUE_OR_POP:
     225             :                 case POP_JUMP_FORWARD_IF_FALSE:
     226             :                 case POP_JUMP_BACKWARD_IF_FALSE:
     227             :                 case POP_JUMP_FORWARD_IF_TRUE:
     228             :                 case POP_JUMP_BACKWARD_IF_TRUE:
     229             :                 {
     230             :                     int64_t target_stack;
     231          12 :                     int j = get_arg(code, i);
     232          12 :                     if (opcode == POP_JUMP_FORWARD_IF_FALSE ||
     233             :                         opcode == POP_JUMP_FORWARD_IF_TRUE) {
     234          10 :                         j += i + 1;
     235             :                     }
     236           2 :                     else if (opcode == POP_JUMP_BACKWARD_IF_FALSE ||
     237             :                              opcode == POP_JUMP_BACKWARD_IF_TRUE) {
     238           2 :                         j = i + 1 - j;
     239             :                     }
     240          12 :                     assert(j < len);
     241          12 :                     if (stacks[j] == UNINITIALIZED && j < i) {
     242           0 :                         todo = 1;
     243             :                     }
     244          12 :                     if (opcode == JUMP_IF_FALSE_OR_POP ||
     245             :                         opcode == JUMP_IF_TRUE_OR_POP)
     246             :                     {
     247           0 :                         target_stack = next_stack;
     248           0 :                         next_stack = pop_value(next_stack);
     249             :                     }
     250             :                     else {
     251          12 :                         next_stack = pop_value(next_stack);
     252          12 :                         target_stack = next_stack;
     253             :                     }
     254          12 :                     assert(stacks[j] == UNINITIALIZED || stacks[j] == target_stack);
     255          12 :                     stacks[j] = target_stack;
     256          12 :                     stacks[i+1] = next_stack;
     257          12 :                     break;
     258             :                 }
     259          33 :                 case SEND:
     260          33 :                     j = get_arg(code, i) + i + 1;
     261          33 :                     assert(j < len);
     262          33 :                     assert(stacks[j] == UNINITIALIZED || stacks[j] == pop_value(next_stack));
     263          33 :                     stacks[j] = pop_value(next_stack);
     264          33 :                     stacks[i+1] = next_stack;
     265          33 :                     break;
     266           3 :                 case JUMP_FORWARD:
     267           3 :                     j = get_arg(code, i) + i + 1;
     268           3 :                     assert(j < len);
     269           3 :                     assert(stacks[j] == UNINITIALIZED || stacks[j] == next_stack);
     270           3 :                     stacks[j] = next_stack;
     271           3 :                     break;
     272          55 :                 case JUMP_BACKWARD:
     273             :                 case JUMP_BACKWARD_NO_INTERRUPT:
     274          55 :                     j = i + 1 - get_arg(code, i);
     275          55 :                     assert(j >= 0);
     276          55 :                     assert(j < len);
     277          55 :                     if (stacks[j] == UNINITIALIZED && j < i) {
     278           0 :                         todo = 1;
     279             :                     }
     280          55 :                     assert(stacks[j] == UNINITIALIZED || stacks[j] == next_stack);
     281          55 :                     stacks[j] = next_stack;
     282          55 :                     break;
     283          30 :                 case GET_ITER:
     284             :                 case GET_AITER:
     285          30 :                     next_stack = push_value(pop_value(next_stack), Iterator);
     286          30 :                     stacks[i+1] = next_stack;
     287          30 :                     break;
     288          18 :                 case FOR_ITER:
     289             :                 {
     290          18 :                     int64_t target_stack = pop_value(next_stack);
     291          18 :                     stacks[i+1] = push_value(next_stack, Object);
     292          18 :                     j = get_arg(code, i) + 1 + INLINE_CACHE_ENTRIES_FOR_ITER + i;
     293          18 :                     assert(j < len);
     294          18 :                     assert(stacks[j] == UNINITIALIZED || stacks[j] == target_stack);
     295          18 :                     stacks[j] = target_stack;
     296          18 :                     break;
     297             :                 }
     298           0 :                 case END_ASYNC_FOR:
     299           0 :                     next_stack = pop_value(pop_value(pop_value(next_stack)));
     300           0 :                     stacks[i+1] = next_stack;
     301           0 :                     break;
     302           0 :                 case PUSH_EXC_INFO:
     303             :                 case POP_EXCEPT:
     304             :                     /* These instructions only appear in exception handlers, which
     305             :                      * skip this switch ever since the move to zero-cost exceptions
     306             :                      * (their stack remains UNINITIALIZED because nothing sets it).
     307             :                      *
     308             :                      * Note that explain_incompatible_stack interprets an
     309             :                      * UNINITIALIZED stack as belonging to an exception handler.
     310             :                      */
     311           0 :                     Py_UNREACHABLE();
     312             :                     break;
     313          90 :                 case RETURN_VALUE:
     314             :                 case RAISE_VARARGS:
     315             :                 case RERAISE:
     316             :                     /* End of block */
     317          90 :                     break;
     318           4 :                 case PUSH_NULL:
     319           4 :                     next_stack = push_value(next_stack, Null);
     320           4 :                     stacks[i+1] = next_stack;
     321           4 :                     break;
     322          54 :                 case LOAD_GLOBAL:
     323             :                 {
     324          54 :                     int j = get_arg(code, i);
     325          54 :                     if (j & 1) {
     326          51 :                         next_stack = push_value(next_stack, Null);
     327             :                     }
     328          54 :                     next_stack = push_value(next_stack, Object);
     329          54 :                     stacks[i+1] = next_stack;
     330          54 :                     break;
     331             :                 }
     332         227 :                 case LOAD_ATTR:
     333             :                 {
     334         227 :                     int j = get_arg(code, i);
     335         227 :                     if (j & 1) {
     336         227 :                         next_stack = pop_value(next_stack);
     337         227 :                         next_stack = push_value(next_stack, Null);
     338         227 :                         next_stack = push_value(next_stack, Object);
     339             :                     }
     340         227 :                     stacks[i+1] = next_stack;
     341         227 :                     break;
     342             :                 }
     343        5468 :                 default:
     344             :                 {
     345        5468 :                     int delta = PyCompile_OpcodeStackEffect(opcode, _Py_OPARG(code[i]));
     346        6551 :                     while (delta < 0) {
     347        1083 :                         next_stack = pop_value(next_stack);
     348        1083 :                         delta++;
     349             :                     }
     350        6336 :                     while (delta > 0) {
     351         868 :                         next_stack = push_value(next_stack, Object);
     352         868 :                         delta--;
     353             :                     }
     354        5468 :                     stacks[i+1] = next_stack;
     355             :                 }
     356             :             }
     357             :         }
     358             :     }
     359          88 :     Py_DECREF(co_code);
     360          88 :     return stacks;
     361             : }
     362             : 
     363             : static int
     364         101 : compatible_stack(int64_t from_stack, int64_t to_stack)
     365             : {
     366         101 :     if (from_stack < 0 || to_stack < 0) {
     367          29 :         return 0;
     368             :     }
     369         102 :     while(from_stack > to_stack) {
     370          30 :         from_stack = pop_value(from_stack);
     371             :     }
     372          80 :     while(from_stack) {
     373          11 :         Kind from_top = top_of_stack(from_stack);
     374          11 :         Kind to_top = top_of_stack(to_stack);
     375          11 :         if (!compatible_kind(from_top, to_top)) {
     376           3 :             return 0;
     377             :         }
     378           8 :         from_stack = pop_value(from_stack);
     379           8 :         to_stack = pop_value(to_stack);
     380             :     }
     381          69 :     return to_stack == 0;
     382             : }
     383             : 
     384             : static const char *
     385          22 : explain_incompatible_stack(int64_t to_stack)
     386             : {
     387          22 :     assert(to_stack != 0);
     388          22 :     if (to_stack == OVERFLOWED) {
     389           0 :         return "stack is too deep to analyze";
     390             :     }
     391          22 :     if (to_stack == UNINITIALIZED) {
     392          10 :         return "can't jump into an exception handler, or code may be unreachable";
     393             :     }
     394          12 :     Kind target_kind = top_of_stack(to_stack);
     395          12 :     switch(target_kind) {
     396           0 :         case Except:
     397           0 :             return "can't jump into an 'except' block as there's no exception";
     398           7 :         case Object:
     399             :         case Null:
     400           7 :             return "incompatible stacks";
     401           5 :         case Iterator:
     402           5 :             return "can't jump into the body of a for loop";
     403           0 :         default:
     404           0 :             Py_UNREACHABLE();
     405             :     }
     406             : }
     407             : 
     408             : static int *
     409          91 : marklines(PyCodeObject *code, int len)
     410             : {
     411             :     PyCodeAddressRange bounds;
     412          91 :     _PyCode_InitAddressRange(code, &bounds);
     413          91 :     assert (bounds.ar_end == 0);
     414          91 :     int last_line = -1;
     415             : 
     416          91 :     int *linestarts = PyMem_New(int, len);
     417          91 :     if (linestarts == NULL) {
     418           0 :         return NULL;
     419             :     }
     420        8240 :     for (int i = 0; i < len; i++) {
     421        8149 :         linestarts[i] = -1;
     422             :     }
     423             : 
     424        3916 :     while (_PyLineTable_NextAddressRange(&bounds)) {
     425        3825 :         assert(bounds.ar_start / (int)sizeof(_Py_CODEUNIT) < len);
     426        3825 :         if (bounds.ar_line != last_line && bounds.ar_line != -1) {
     427         692 :             linestarts[bounds.ar_start / sizeof(_Py_CODEUNIT)] = bounds.ar_line;
     428         692 :             last_line = bounds.ar_line;
     429             :         }
     430             :     }
     431          91 :     return linestarts;
     432             : }
     433             : 
     434             : static int
     435          91 : first_line_not_before(int *lines, int len, int line)
     436             : {
     437          91 :     int result = INT_MAX;
     438        8240 :     for (int i = 0; i < len; i++) {
     439        8149 :         if (lines[i] < result && lines[i] >= line) {
     440          98 :             result = lines[i];
     441             :         }
     442             :     }
     443          91 :     if (result == INT_MAX) {
     444           3 :         return -1;
     445             :     }
     446          88 :     return result;
     447             : }
     448             : 
     449             : static void
     450          26 : frame_stack_pop(PyFrameObject *f)
     451             : {
     452          26 :     PyObject *v = _PyFrame_StackPop(f->f_frame);
     453          26 :     Py_XDECREF(v);
     454          26 : }
     455             : 
     456             : static PyFrameState
     457         317 : _PyFrame_GetState(PyFrameObject *frame)
     458             : {
     459         317 :     if (frame->f_frame->stacktop == 0) {
     460          96 :         return FRAME_CLEARED;
     461             :     }
     462         221 :     switch(frame->f_frame->owner) {
     463          45 :         case FRAME_OWNED_BY_GENERATOR:
     464             :         {
     465          45 :             PyGenObject *gen = _PyFrame_GetGenerator(frame->f_frame);
     466          45 :             return gen->gi_frame_state;
     467             :         }
     468         176 :         case FRAME_OWNED_BY_THREAD:
     469             :         {
     470         176 :             if (_PyInterpreterFrame_LASTI(frame->f_frame) < 0) {
     471           0 :                 return FRAME_CREATED;
     472             :             }
     473         176 :             switch (_PyOpcode_Deopt[_Py_OPCODE(*frame->f_frame->prev_instr)])
     474             :             {
     475           0 :                 case COPY_FREE_VARS:
     476             :                 case MAKE_CELL:
     477             :                 case RETURN_GENERATOR:
     478             :                     /* Frame not fully initialized */
     479           0 :                     return FRAME_CREATED;
     480         176 :                 default:
     481         176 :                     return FRAME_EXECUTING;
     482             :             }
     483             :         }
     484           0 :         case FRAME_OWNED_BY_FRAME_OBJECT:
     485           0 :             return FRAME_COMPLETED;
     486             :     }
     487           0 :     Py_UNREACHABLE();
     488             : }
     489             : 
     490             : static void
     491          94 : add_load_fast_null_checks(PyCodeObject *co)
     492             : {
     493          94 :     int changed = 0;
     494          94 :     _Py_CODEUNIT *instructions = _PyCode_CODE(co);
     495        9471 :     for (Py_ssize_t i = 0; i < Py_SIZE(co); i++) {
     496        9377 :         switch (_Py_OPCODE(instructions[i])) {
     497         380 :             case LOAD_FAST:
     498             :             case LOAD_FAST__LOAD_FAST:
     499             :             case LOAD_FAST__LOAD_CONST:
     500         380 :                 changed = 1;
     501         380 :                 _Py_SET_OPCODE(instructions[i], LOAD_FAST_CHECK);
     502         380 :                 break;
     503           0 :             case LOAD_CONST__LOAD_FAST:
     504           0 :                 changed = 1;
     505           0 :                 _Py_SET_OPCODE(instructions[i], LOAD_CONST);
     506           0 :                 break;
     507           1 :             case STORE_FAST__LOAD_FAST:
     508           1 :                 changed = 1;
     509           1 :                 _Py_SET_OPCODE(instructions[i], STORE_FAST);
     510           1 :                 break;
     511             :         }
     512        9377 :     }
     513          94 :     if (changed) {
     514             :         // invalidate cached co_code object
     515          86 :         Py_CLEAR(co->_co_code);
     516             :     }
     517          94 : }
     518             : 
     519             : /* Setter for f_lineno - you can set f_lineno from within a trace function in
     520             :  * order to jump to a given line of code, subject to some restrictions.  Most
     521             :  * lines are OK to jump to because they don't make any assumptions about the
     522             :  * state of the stack (obvious because you could remove the line and the code
     523             :  * would still work without any stack errors), but there are some constructs
     524             :  * that limit jumping:
     525             :  *
     526             :  *  o Any exception handlers.
     527             :  *  o 'for' and 'async for' loops can't be jumped into because the
     528             :  *    iterator needs to be on the stack.
     529             :  *  o Jumps cannot be made from within a trace function invoked with a
     530             :  *    'return' or 'exception' event since the eval loop has been exited at
     531             :  *    that time.
     532             :  */
     533             : static int
     534          98 : frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignored))
     535             : {
     536          98 :     if (p_new_lineno == NULL) {
     537           1 :         PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
     538           1 :         return -1;
     539             :     }
     540             :     /* f_lineno must be an integer. */
     541          97 :     if (!PyLong_CheckExact(p_new_lineno)) {
     542           1 :         PyErr_SetString(PyExc_ValueError,
     543             :                         "lineno must be an integer");
     544           1 :         return -1;
     545             :     }
     546             : 
     547          96 :     PyFrameState state = _PyFrame_GetState(f);
     548             :     /*
     549             :      * This code preserves the historical restrictions on
     550             :      * setting the line number of a frame.
     551             :      * Jumps are forbidden on a 'return' trace event (except after a yield).
     552             :      * Jumps from 'call' trace events are also forbidden.
     553             :      * In addition, jumps are forbidden when not tracing,
     554             :      * as this is a debugging feature.
     555             :      */
     556          96 :     switch(PyThreadState_GET()->tracing_what) {
     557           1 :         case PyTrace_EXCEPTION:
     558           1 :             PyErr_SetString(PyExc_ValueError,
     559             :                 "can only jump from a 'line' trace event");
     560           1 :             return -1;
     561           2 :         case PyTrace_CALL:
     562           2 :             PyErr_Format(PyExc_ValueError,
     563             :                      "can't jump from the 'call' trace event of a new frame");
     564           2 :             return -1;
     565          91 :         case PyTrace_LINE:
     566          91 :             break;
     567           2 :         case PyTrace_RETURN:
     568           2 :             if (state == FRAME_SUSPENDED) {
     569           1 :                 break;
     570             :             }
     571             :             /* fall through */
     572             :         default:
     573           1 :             PyErr_SetString(PyExc_ValueError,
     574             :                 "can only jump from a 'line' trace event");
     575           1 :             return -1;
     576             :     }
     577          92 :     if (!f->f_trace) {
     578           0 :         PyErr_Format(PyExc_ValueError,
     579             :                     "f_lineno can only be set by a trace function");
     580           0 :         return -1;
     581             :     }
     582             : 
     583             :     int new_lineno;
     584             : 
     585             :     /* Fail if the line falls outside the code block and
     586             :         select first line with actual code. */
     587             :     int overflow;
     588          92 :     long l_new_lineno = PyLong_AsLongAndOverflow(p_new_lineno, &overflow);
     589          92 :     if (overflow
     590             : #if SIZEOF_LONG > SIZEOF_INT
     591          92 :         || l_new_lineno > INT_MAX
     592          92 :         || l_new_lineno < INT_MIN
     593             : #endif
     594             :     ) {
     595           0 :         PyErr_SetString(PyExc_ValueError,
     596             :                         "lineno out of range");
     597           0 :         return -1;
     598             :     }
     599          92 :     new_lineno = (int)l_new_lineno;
     600             : 
     601          92 :     if (new_lineno < f->f_frame->f_code->co_firstlineno) {
     602           1 :         PyErr_Format(PyExc_ValueError,
     603             :                     "line %d comes before the current code block",
     604             :                     new_lineno);
     605           1 :         return -1;
     606             :     }
     607             : 
     608          91 :     add_load_fast_null_checks(f->f_frame->f_code);
     609             : 
     610             :     /* PyCode_NewWithPosOnlyArgs limits co_code to be under INT_MAX so this
     611             :      * should never overflow. */
     612          91 :     int len = (int)Py_SIZE(f->f_frame->f_code);
     613          91 :     int *lines = marklines(f->f_frame->f_code, len);
     614          91 :     if (lines == NULL) {
     615           0 :         return -1;
     616             :     }
     617             : 
     618          91 :     new_lineno = first_line_not_before(lines, len, new_lineno);
     619          91 :     if (new_lineno < 0) {
     620           3 :         PyErr_Format(PyExc_ValueError,
     621             :                     "line %d comes after the current code block",
     622             :                     (int)l_new_lineno);
     623           3 :         PyMem_Free(lines);
     624           3 :         return -1;
     625             :     }
     626             : 
     627          88 :     int64_t *stacks = mark_stacks(f->f_frame->f_code, len);
     628          88 :     if (stacks == NULL) {
     629           0 :         PyMem_Free(lines);
     630           0 :         return -1;
     631             :     }
     632             : 
     633          88 :     int64_t best_stack = OVERFLOWED;
     634          88 :     int best_addr = -1;
     635          88 :     int64_t start_stack = stacks[_PyInterpreterFrame_LASTI(f->f_frame)];
     636          88 :     int err = -1;
     637          88 :     const char *msg = "cannot find bytecode for specified line";
     638        8057 :     for (int i = 0; i < len; i++) {
     639        7969 :         if (lines[i] == new_lineno) {
     640         101 :             int64_t target_stack = stacks[i];
     641         101 :             if (compatible_stack(start_stack, target_stack)) {
     642          58 :                 err = 0;
     643          58 :                 if (target_stack > best_stack) {
     644          58 :                     best_stack = target_stack;
     645          58 :                     best_addr = i;
     646             :                 }
     647             :             }
     648          43 :             else if (err < 0) {
     649          32 :                 if (start_stack == OVERFLOWED) {
     650           0 :                     msg = "stack to deep to analyze";
     651             :                 }
     652          32 :                 else if (start_stack == UNINITIALIZED) {
     653          10 :                     msg = "can't jump from within an exception handler";
     654             :                 }
     655             :                 else {
     656          22 :                     msg = explain_incompatible_stack(target_stack);
     657          22 :                     err = 1;
     658             :                 }
     659             :             }
     660             :         }
     661             :     }
     662          88 :     PyMem_Free(stacks);
     663          88 :     PyMem_Free(lines);
     664          88 :     if (err) {
     665          30 :         PyErr_SetString(PyExc_ValueError, msg);
     666          30 :         return -1;
     667             :     }
     668          58 :     if (state == FRAME_SUSPENDED) {
     669             :         /* Account for value popped by yield */
     670           1 :         start_stack = pop_value(start_stack);
     671             :     }
     672          84 :     while (start_stack > best_stack) {
     673          26 :         frame_stack_pop(f);
     674          26 :         start_stack = pop_value(start_stack);
     675             :     }
     676             :     /* Finally set the new lasti and return OK. */
     677          58 :     f->f_lineno = 0;
     678          58 :     f->f_frame->prev_instr = _PyCode_CODE(f->f_frame->f_code) + best_addr;
     679          58 :     return 0;
     680             : }
     681             : 
     682             : static PyObject *
     683           8 : frame_gettrace(PyFrameObject *f, void *closure)
     684             : {
     685           8 :     PyObject* trace = f->f_trace;
     686             : 
     687           8 :     if (trace == NULL)
     688           3 :         trace = Py_None;
     689             : 
     690           8 :     Py_INCREF(trace);
     691             : 
     692           8 :     return trace;
     693             : }
     694             : 
     695             : static int
     696        1787 : frame_settrace(PyFrameObject *f, PyObject* v, void *closure)
     697             : {
     698        1787 :     if (v == Py_None) {
     699           3 :         v = NULL;
     700             :     }
     701        1787 :     Py_XINCREF(v);
     702        1787 :     Py_XSETREF(f->f_trace, v);
     703             : 
     704        1787 :     return 0;
     705             : }
     706             : 
     707             : 
     708             : static PyGetSetDef frame_getsetlist[] = {
     709             :     {"f_back",          (getter)frame_getback, NULL, NULL},
     710             :     {"f_locals",        (getter)frame_getlocals, NULL, NULL},
     711             :     {"f_lineno",        (getter)frame_getlineno,
     712             :                     (setter)frame_setlineno, NULL},
     713             :     {"f_trace",         (getter)frame_gettrace, (setter)frame_settrace, NULL},
     714             :     {"f_lasti",         (getter)frame_getlasti, NULL, NULL},
     715             :     {"f_globals",       (getter)frame_getglobals, NULL, NULL},
     716             :     {"f_builtins",      (getter)frame_getbuiltins, NULL, NULL},
     717             :     {"f_code",          (getter)frame_getcode, NULL, NULL},
     718             :     {0}
     719             : };
     720             : 
     721             : /* Stack frames are allocated and deallocated at a considerable rate.
     722             :    In an attempt to improve the speed of function calls, we maintain
     723             :    a separate free list of stack frames (just like floats are
     724             :    allocated in a special way -- see floatobject.c).  When a stack
     725             :    frame is on the free list, only the following members have a meaning:
     726             :     ob_type             == &Frametype
     727             :     f_back              next item on free list, or NULL
     728             : */
     729             : 
     730             : static void
     731     5186170 : frame_dealloc(PyFrameObject *f)
     732             : {
     733             :     /* It is the responsibility of the owning generator/coroutine
     734             :      * to have cleared the generator pointer */
     735             : 
     736     5186170 :     assert(f->f_frame->owner != FRAME_OWNED_BY_GENERATOR ||
     737             :         _PyFrame_GetGenerator(f->f_frame)->gi_frame_state == FRAME_CLEARED);
     738             : 
     739     5186170 :     if (_PyObject_GC_IS_TRACKED(f)) {
     740     1715630 :         _PyObject_GC_UNTRACK(f);
     741             :     }
     742             : 
     743     5186170 :     Py_TRASHCAN_BEGIN(f, frame_dealloc);
     744     5185410 :     PyCodeObject *co = NULL;
     745             : 
     746             :     /* Kill all local variables including specials, if we own them */
     747     5185410 :     if (f->f_frame->owner == FRAME_OWNED_BY_FRAME_OBJECT) {
     748     1715630 :         assert(f->f_frame == (_PyInterpreterFrame *)f->_f_frame_data);
     749     1715630 :         _PyInterpreterFrame *frame = (_PyInterpreterFrame *)f->_f_frame_data;
     750             :         /* Don't clear code object until the end */
     751     1715630 :         co = frame->f_code;
     752     1715630 :         frame->f_code = NULL;
     753     1715630 :         Py_CLEAR(frame->f_func);
     754     1715630 :         Py_CLEAR(frame->f_locals);
     755     1715630 :         PyObject **locals = _PyFrame_GetLocalsArray(frame);
     756     9887380 :         for (int i = 0; i < frame->stacktop; i++) {
     757     8171750 :             Py_CLEAR(locals[i]);
     758             :         }
     759             :     }
     760     5185410 :     Py_CLEAR(f->f_back);
     761     5185410 :     Py_CLEAR(f->f_trace);
     762     5185410 :     PyObject_GC_Del(f);
     763     5185410 :     Py_XDECREF(co);
     764     5185410 :     Py_TRASHCAN_END;
     765     5186170 : }
     766             : 
     767             : static int
     768      171469 : frame_traverse(PyFrameObject *f, visitproc visit, void *arg)
     769             : {
     770      171469 :     Py_VISIT(f->f_back);
     771      171469 :     Py_VISIT(f->f_trace);
     772      171469 :     if (f->f_frame->owner != FRAME_OWNED_BY_FRAME_OBJECT) {
     773           0 :         return 0;
     774             :     }
     775      171469 :     assert(f->f_frame->frame_obj == NULL);
     776      171469 :     return _PyFrame_Traverse(f->f_frame, visit, arg);
     777             : }
     778             : 
     779             : static int
     780       74173 : frame_tp_clear(PyFrameObject *f)
     781             : {
     782       74173 :     Py_CLEAR(f->f_trace);
     783             : 
     784             :     /* locals and stack */
     785       74173 :     PyObject **locals = _PyFrame_GetLocalsArray(f->f_frame);
     786       74173 :     assert(f->f_frame->stacktop >= 0);
     787      625201 :     for (int i = 0; i < f->f_frame->stacktop; i++) {
     788      551028 :         Py_CLEAR(locals[i]);
     789             :     }
     790       74173 :     f->f_frame->stacktop = 0;
     791       74173 :     return 0;
     792             : }
     793             : 
     794             : static PyObject *
     795      261371 : frame_clear(PyFrameObject *f, PyObject *Py_UNUSED(ignored))
     796             : {
     797      261371 :     if (f->f_frame->owner == FRAME_OWNED_BY_GENERATOR) {
     798         723 :         PyGenObject *gen = _PyFrame_GetGenerator(f->f_frame);
     799         723 :         if (gen->gi_frame_state == FRAME_EXECUTING) {
     800         720 :             goto running;
     801             :         }
     802           3 :         _PyGen_Finalize((PyObject *)gen);
     803             :     }
     804      260648 :     else if (f->f_frame->owner == FRAME_OWNED_BY_THREAD) {
     805      187510 :         goto running;
     806             :     }
     807             :     else {
     808       73138 :         assert(f->f_frame->owner == FRAME_OWNED_BY_FRAME_OBJECT);
     809       73138 :         (void)frame_tp_clear(f);
     810             :     }
     811       73141 :     Py_RETURN_NONE;
     812      188230 : running:
     813      188230 :     PyErr_SetString(PyExc_RuntimeError,
     814             :                     "cannot clear an executing frame");
     815      188230 :     return NULL;
     816             : }
     817             : 
     818             : PyDoc_STRVAR(clear__doc__,
     819             : "F.clear(): clear most references held by the frame");
     820             : 
     821             : static PyObject *
     822           1 : frame_sizeof(PyFrameObject *f, PyObject *Py_UNUSED(ignored))
     823             : {
     824             :     Py_ssize_t res;
     825           1 :     res = offsetof(PyFrameObject, _f_frame_data) + offsetof(_PyInterpreterFrame, localsplus);
     826           1 :     PyCodeObject *code = f->f_frame->f_code;
     827           1 :     res += (code->co_nlocalsplus+code->co_stacksize) * sizeof(PyObject *);
     828           1 :     return PyLong_FromSsize_t(res);
     829             : }
     830             : 
     831             : PyDoc_STRVAR(sizeof__doc__,
     832             : "F.__sizeof__() -> size of F in memory, in bytes");
     833             : 
     834             : static PyObject *
     835          62 : frame_repr(PyFrameObject *f)
     836             : {
     837          62 :     int lineno = PyFrame_GetLineNumber(f);
     838          62 :     PyCodeObject *code = f->f_frame->f_code;
     839          62 :     return PyUnicode_FromFormat(
     840             :         "<frame at %p, file %R, line %d, code %S>",
     841             :         f, code->co_filename, lineno, code->co_name);
     842             : }
     843             : 
     844             : static PyMethodDef frame_methods[] = {
     845             :     {"clear",           (PyCFunction)frame_clear,       METH_NOARGS,
     846             :      clear__doc__},
     847             :     {"__sizeof__",      (PyCFunction)frame_sizeof,      METH_NOARGS,
     848             :      sizeof__doc__},
     849             :     {NULL,              NULL}   /* sentinel */
     850             : };
     851             : 
     852             : PyTypeObject PyFrame_Type = {
     853             :     PyVarObject_HEAD_INIT(&PyType_Type, 0)
     854             :     "frame",
     855             :     offsetof(PyFrameObject, _f_frame_data) +
     856             :     offsetof(_PyInterpreterFrame, localsplus),
     857             :     sizeof(PyObject *),
     858             :     (destructor)frame_dealloc,                  /* tp_dealloc */
     859             :     0,                                          /* tp_vectorcall_offset */
     860             :     0,                                          /* tp_getattr */
     861             :     0,                                          /* tp_setattr */
     862             :     0,                                          /* tp_as_async */
     863             :     (reprfunc)frame_repr,                       /* tp_repr */
     864             :     0,                                          /* tp_as_number */
     865             :     0,                                          /* tp_as_sequence */
     866             :     0,                                          /* tp_as_mapping */
     867             :     0,                                          /* tp_hash */
     868             :     0,                                          /* tp_call */
     869             :     0,                                          /* tp_str */
     870             :     PyObject_GenericGetAttr,                    /* tp_getattro */
     871             :     PyObject_GenericSetAttr,                    /* tp_setattro */
     872             :     0,                                          /* tp_as_buffer */
     873             :     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
     874             :     0,                                          /* tp_doc */
     875             :     (traverseproc)frame_traverse,               /* tp_traverse */
     876             :     (inquiry)frame_tp_clear,                    /* tp_clear */
     877             :     0,                                          /* tp_richcompare */
     878             :     0,                                          /* tp_weaklistoffset */
     879             :     0,                                          /* tp_iter */
     880             :     0,                                          /* tp_iternext */
     881             :     frame_methods,                              /* tp_methods */
     882             :     frame_memberlist,                           /* tp_members */
     883             :     frame_getsetlist,                           /* tp_getset */
     884             :     0,                                          /* tp_base */
     885             :     0,                                          /* tp_dict */
     886             : };
     887             : 
     888             : static void
     889          30 : init_frame(_PyInterpreterFrame *frame, PyFunctionObject *func, PyObject *locals)
     890             : {
     891             :     /* _PyFrame_InitializeSpecials consumes reference to func */
     892          30 :     Py_INCREF(func);
     893          30 :     Py_XINCREF(locals);
     894          30 :     PyCodeObject *code = (PyCodeObject *)func->func_code;
     895          30 :     _PyFrame_InitializeSpecials(frame, func, locals, code);
     896          30 :     for (Py_ssize_t i = 0; i < code->co_nlocalsplus; i++) {
     897           0 :         frame->localsplus[i] = NULL;
     898             :     }
     899          30 : }
     900             : 
     901             : PyFrameObject*
     902     5185480 : _PyFrame_New_NoTrack(PyCodeObject *code)
     903             : {
     904             :     CALL_STAT_INC(frame_objects_created);
     905     5185480 :     int slots = code->co_nlocalsplus + code->co_stacksize;
     906     5185480 :     PyFrameObject *f = PyObject_GC_NewVar(PyFrameObject, &PyFrame_Type, slots);
     907     5185480 :     if (f == NULL) {
     908          38 :         return NULL;
     909             :     }
     910     5185440 :     f->f_back = NULL;
     911     5185440 :     f->f_trace = NULL;
     912     5185440 :     f->f_trace_lines = 1;
     913     5185440 :     f->f_trace_opcodes = 0;
     914     5185440 :     f->f_fast_as_locals = 0;
     915     5185440 :     f->f_lineno = 0;
     916     5185440 :     return f;
     917             : }
     918             : 
     919             : /* Legacy API */
     920             : PyFrameObject*
     921          30 : PyFrame_New(PyThreadState *tstate, PyCodeObject *code,
     922             :             PyObject *globals, PyObject *locals)
     923             : {
     924          30 :     PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
     925          30 :     if (builtins == NULL) {
     926           0 :         return NULL;
     927             :     }
     928          30 :     PyFrameConstructor desc = {
     929             :         .fc_globals = globals,
     930             :         .fc_builtins = builtins,
     931          30 :         .fc_name = code->co_name,
     932          30 :         .fc_qualname = code->co_name,
     933             :         .fc_code = (PyObject *)code,
     934             :         .fc_defaults = NULL,
     935             :         .fc_kwdefaults = NULL,
     936             :         .fc_closure = NULL
     937             :     };
     938          30 :     PyFunctionObject *func = _PyFunction_FromConstructor(&desc);
     939          30 :     if (func == NULL) {
     940           0 :         return NULL;
     941             :     }
     942          30 :     PyFrameObject *f = _PyFrame_New_NoTrack(code);
     943          30 :     if (f == NULL) {
     944           0 :         Py_DECREF(func);
     945           0 :         return NULL;
     946             :     }
     947          30 :     init_frame((_PyInterpreterFrame *)f->_f_frame_data, func, locals);
     948          30 :     f->f_frame = (_PyInterpreterFrame *)f->_f_frame_data;
     949          30 :     f->f_frame->owner = FRAME_OWNED_BY_FRAME_OBJECT;
     950          30 :     Py_DECREF(func);
     951          30 :     _PyObject_GC_TRACK(f);
     952          30 :     return f;
     953             : }
     954             : 
     955             : static int
     956       16151 : _PyFrame_OpAlreadyRan(_PyInterpreterFrame *frame, int opcode, int oparg)
     957             : {
     958             :     // This only works when opcode is a non-quickened form:
     959       16151 :     assert(_PyOpcode_Deopt[opcode] == opcode);
     960       16151 :     int check_oparg = 0;
     961       16151 :     for (_Py_CODEUNIT *instruction = _PyCode_CODE(frame->f_code);
     962       23710 :          instruction < frame->prev_instr; instruction++)
     963             :     {
     964       23710 :         int check_opcode = _PyOpcode_Deopt[_Py_OPCODE(*instruction)];
     965       23710 :         check_oparg |= _Py_OPARG(*instruction);
     966       23710 :         if (check_opcode == opcode && check_oparg == oparg) {
     967       16151 :             return 1;
     968             :         }
     969        7559 :         if (check_opcode == EXTENDED_ARG) {
     970           0 :             check_oparg <<= 8;
     971             :         }
     972             :         else {
     973        7559 :             check_oparg = 0;
     974             :         }
     975        7559 :         instruction += _PyOpcode_Caches[check_opcode];
     976             :     }
     977           0 :     return 0;
     978             : }
     979             : 
     980             : int
     981       63142 : _PyFrame_FastToLocalsWithError(_PyInterpreterFrame *frame) {
     982             :     /* Merge fast locals into f->f_locals */
     983             :     PyObject *locals;
     984             :     PyObject **fast;
     985             :     PyCodeObject *co;
     986       63142 :     locals = frame->f_locals;
     987       63142 :     if (locals == NULL) {
     988       12969 :         locals = frame->f_locals = PyDict_New();
     989       12969 :         if (locals == NULL)
     990           0 :             return -1;
     991             :     }
     992       63142 :     co = frame->f_code;
     993       63142 :     fast = _PyFrame_GetLocalsArray(frame);
     994             :     // COPY_FREE_VARS has no quickened forms, so no need to use _PyOpcode_Deopt
     995             :     // here:
     996       63142 :     int lasti = _PyInterpreterFrame_LASTI(frame);
     997       63142 :     if (lasti < 0 && _Py_OPCODE(_PyCode_CODE(co)[0]) == COPY_FREE_VARS) {
     998             :         /* Free vars have not been initialized -- Do that */
     999           0 :         PyCodeObject *co = frame->f_code;
    1000           0 :         PyObject *closure = frame->f_func->func_closure;
    1001           0 :         int offset = co->co_nlocals + co->co_nplaincellvars;
    1002           0 :         for (int i = 0; i < co->co_nfreevars; ++i) {
    1003           0 :             PyObject *o = PyTuple_GET_ITEM(closure, i);
    1004           0 :             Py_INCREF(o);
    1005           0 :             frame->localsplus[offset + i] = o;
    1006             :         }
    1007             :         // COPY_FREE_VARS doesn't have inline CACHEs, either:
    1008           0 :         frame->prev_instr = _PyCode_CODE(frame->f_code);
    1009             :     }
    1010      310709 :     for (int i = 0; i < co->co_nlocalsplus; i++) {
    1011      247567 :         _PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i);
    1012             : 
    1013             :         /* If the namespace is unoptimized, then one of the
    1014             :            following cases applies:
    1015             :            1. It does not contain free variables, because it
    1016             :               uses import * or is a top-level namespace.
    1017             :            2. It is a class namespace.
    1018             :            We don't want to accidentally copy free variables
    1019             :            into the locals dict used by the class.
    1020             :         */
    1021      247567 :         if (kind & CO_FAST_FREE && !(co->co_flags & CO_OPTIMIZED)) {
    1022           5 :             continue;
    1023             :         }
    1024             : 
    1025      247562 :         PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
    1026      247562 :         PyObject *value = fast[i];
    1027      247562 :         if (frame->stacktop) {
    1028      247545 :             if (kind & CO_FAST_FREE) {
    1029             :                 // The cell was set by COPY_FREE_VARS.
    1030         247 :                 assert(value != NULL && PyCell_Check(value));
    1031         247 :                 value = PyCell_GET(value);
    1032             :             }
    1033      247298 :             else if (kind & CO_FAST_CELL) {
    1034             :                 // Note that no *_DEREF ops can happen before MAKE_CELL
    1035             :                 // executes.  So there's no need to duplicate the work
    1036             :                 // that MAKE_CELL would otherwise do later, if it hasn't
    1037             :                 // run yet.
    1038       16145 :                 if (value != NULL) {
    1039       32290 :                     if (PyCell_Check(value) &&
    1040       16145 :                             _PyFrame_OpAlreadyRan(frame, MAKE_CELL, i)) {
    1041             :                         // (likely) MAKE_CELL must have executed already.
    1042       16145 :                         value = PyCell_GET(value);
    1043             :                     }
    1044             :                     // (likely) Otherwise it it is an arg (kind & CO_FAST_LOCAL),
    1045             :                     // with the initial value set when the frame was created...
    1046             :                     // (unlikely) ...or it was set to some initial value by
    1047             :                     // an earlier call to PyFrame_LocalsToFast().
    1048             :                 }
    1049             :             }
    1050             :         }
    1051             :         else {
    1052          17 :             assert(value == NULL);
    1053             :         }
    1054      247562 :         if (value == NULL) {
    1055       38822 :             if (PyObject_DelItem(locals, name) != 0) {
    1056       38816 :                 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
    1057       38816 :                     PyErr_Clear();
    1058             :                 }
    1059             :                 else {
    1060           0 :                     return -1;
    1061             :                 }
    1062             :             }
    1063             :         }
    1064             :         else {
    1065      208740 :             if (PyObject_SetItem(locals, name, value) != 0) {
    1066           0 :                 return -1;
    1067             :             }
    1068             :         }
    1069             :     }
    1070       63142 :     return 0;
    1071             : }
    1072             : 
    1073             : int
    1074         587 : PyFrame_FastToLocalsWithError(PyFrameObject *f)
    1075             : {
    1076         587 :     if (f == NULL) {
    1077           0 :         PyErr_BadInternalCall();
    1078           0 :         return -1;
    1079             :     }
    1080         587 :     int err = _PyFrame_FastToLocalsWithError(f->f_frame);
    1081         587 :     if (err == 0) {
    1082         587 :         f->f_fast_as_locals = 1;
    1083             :     }
    1084         587 :     return err;
    1085             : }
    1086             : 
    1087             : void
    1088           0 : PyFrame_FastToLocals(PyFrameObject *f)
    1089             : {
    1090             :     int res;
    1091             : 
    1092           0 :     assert(!PyErr_Occurred());
    1093             : 
    1094           0 :     res = PyFrame_FastToLocalsWithError(f);
    1095           0 :     if (res < 0)
    1096           0 :         PyErr_Clear();
    1097           0 : }
    1098             : 
    1099             : void
    1100       41302 : _PyFrame_LocalsToFast(_PyInterpreterFrame *frame, int clear)
    1101             : {
    1102             :     /* Merge locals into fast locals */
    1103             :     PyObject *locals;
    1104             :     PyObject **fast;
    1105             :     PyObject *error_type, *error_value, *error_traceback;
    1106             :     PyCodeObject *co;
    1107       41302 :     locals = frame->f_locals;
    1108       41302 :     if (locals == NULL) {
    1109           0 :         return;
    1110             :     }
    1111       41302 :     fast = _PyFrame_GetLocalsArray(frame);
    1112       41302 :     co = frame->f_code;
    1113       41302 :     bool added_null_checks = false;
    1114             : 
    1115       41302 :     PyErr_Fetch(&error_type, &error_value, &error_traceback);
    1116       41580 :     for (int i = 0; i < co->co_nlocalsplus; i++) {
    1117         278 :         _PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i);
    1118             : 
    1119             :         /* Same test as in PyFrame_FastToLocals() above. */
    1120         278 :         if (kind & CO_FAST_FREE && !(co->co_flags & CO_OPTIMIZED)) {
    1121           0 :             continue;
    1122             :         }
    1123         278 :         PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
    1124         278 :         PyObject *value = PyObject_GetItem(locals, name);
    1125             :         /* We only care about NULLs if clear is true. */
    1126         278 :         if (value == NULL) {
    1127          59 :             PyErr_Clear();
    1128          59 :             if (!clear) {
    1129           0 :                 continue;
    1130             :             }
    1131             :         }
    1132         278 :         PyObject *oldvalue = fast[i];
    1133         278 :         if (!added_null_checks && oldvalue != NULL && value == NULL) {
    1134           3 :             add_load_fast_null_checks(co);
    1135           3 :             added_null_checks = true;
    1136             :         }
    1137         278 :         PyObject *cell = NULL;
    1138         278 :         if (kind == CO_FAST_FREE) {
    1139             :             // The cell was set when the frame was created from
    1140             :             // the function's closure.
    1141           6 :             assert(oldvalue != NULL && PyCell_Check(oldvalue));
    1142           6 :             cell = oldvalue;
    1143             :         }
    1144         272 :         else if (kind & CO_FAST_CELL && oldvalue != NULL) {
    1145             :             /* Same test as in PyFrame_FastToLocals() above. */
    1146          12 :             if (PyCell_Check(oldvalue) &&
    1147           6 :                     _PyFrame_OpAlreadyRan(frame, MAKE_CELL, i)) {
    1148             :                 // (likely) MAKE_CELL must have executed already.
    1149           6 :                 cell = oldvalue;
    1150             :             }
    1151             :             // (unlikely) Otherwise, it must have been set to some
    1152             :             // initial value by an earlier call to PyFrame_LocalsToFast().
    1153             :         }
    1154         278 :         if (cell != NULL) {
    1155          12 :             oldvalue = PyCell_GET(cell);
    1156          12 :             if (value != oldvalue) {
    1157           0 :                 Py_XINCREF(value);
    1158           0 :                 PyCell_SET(cell, value);
    1159           0 :                 Py_XDECREF(oldvalue);
    1160             :             }
    1161             :         }
    1162         266 :         else if (value != oldvalue) {
    1163           3 :             Py_XINCREF(value);
    1164           3 :             Py_XSETREF(fast[i], value);
    1165             :         }
    1166         278 :         Py_XDECREF(value);
    1167             :     }
    1168       41302 :     PyErr_Restore(error_type, error_value, error_traceback);
    1169             : }
    1170             : 
    1171             : void
    1172      886770 : PyFrame_LocalsToFast(PyFrameObject *f, int clear)
    1173             : {
    1174      886770 :     if (f && f->f_fast_as_locals && _PyFrame_GetState(f) != FRAME_CLEARED) {
    1175         126 :         _PyFrame_LocalsToFast(f->f_frame, clear);
    1176         126 :         f->f_fast_as_locals = 0;
    1177             :     }
    1178      886770 : }
    1179             : 
    1180             : 
    1181           0 : int _PyFrame_IsEntryFrame(PyFrameObject *frame)
    1182             : {
    1183           0 :     assert(frame != NULL);
    1184           0 :     return frame->f_frame->is_entry;
    1185             : }
    1186             : 
    1187             : 
    1188             : PyCodeObject *
    1189      927357 : PyFrame_GetCode(PyFrameObject *frame)
    1190             : {
    1191      927357 :     assert(frame != NULL);
    1192      927357 :     PyCodeObject *code = frame->f_frame->f_code;
    1193      927357 :     assert(code != NULL);
    1194      927357 :     Py_INCREF(code);
    1195      927357 :     return code;
    1196             : }
    1197             : 
    1198             : 
    1199             : PyFrameObject*
    1200      144319 : PyFrame_GetBack(PyFrameObject *frame)
    1201             : {
    1202      144319 :     assert(frame != NULL);
    1203      144319 :     PyFrameObject *back = frame->f_back;
    1204      144319 :     if (back == NULL) {
    1205      144283 :         _PyInterpreterFrame *prev = frame->f_frame->previous;
    1206      144287 :         while (prev && _PyFrame_IsIncomplete(prev)) {
    1207           4 :             prev = prev->previous;
    1208             :         }
    1209      144283 :         if (prev) {
    1210      141826 :             back = _PyFrame_GetFrameObject(prev);
    1211             :         }
    1212             :     }
    1213      144319 :     Py_XINCREF(back);
    1214      144319 :     return back;
    1215             : }
    1216             : 
    1217             : PyObject*
    1218           1 : PyFrame_GetLocals(PyFrameObject *frame)
    1219             : {
    1220           1 :     return frame_getlocals(frame, NULL);
    1221             : }
    1222             : 
    1223             : PyObject*
    1224           1 : PyFrame_GetGlobals(PyFrameObject *frame)
    1225             : {
    1226           1 :     return frame_getglobals(frame, NULL);
    1227             : }
    1228             : 
    1229             : PyObject*
    1230           1 : PyFrame_GetBuiltins(PyFrameObject *frame)
    1231             : {
    1232           1 :     return frame_getbuiltins(frame, NULL);
    1233             : }
    1234             : 
    1235             : int
    1236           1 : PyFrame_GetLasti(PyFrameObject *frame)
    1237             : {
    1238           1 :     int lasti = _PyInterpreterFrame_LASTI(frame->f_frame);
    1239           1 :     if (lasti < 0) {
    1240           0 :         return -1;
    1241             :     }
    1242           1 :     return lasti * sizeof(_Py_CODEUNIT);
    1243             : }
    1244             : 
    1245             : PyObject *
    1246           1 : PyFrame_GetGenerator(PyFrameObject *frame)
    1247             : {
    1248           1 :     if (frame->f_frame->owner != FRAME_OWNED_BY_GENERATOR) {
    1249           0 :         return NULL;
    1250             :     }
    1251           1 :     PyGenObject *gen = _PyFrame_GetGenerator(frame->f_frame);
    1252           1 :     return Py_NewRef(gen);
    1253             : }
    1254             : 
    1255             : PyObject*
    1256    15725600 : _PyEval_BuiltinsFromGlobals(PyThreadState *tstate, PyObject *globals)
    1257             : {
    1258    15725600 :     PyObject *builtins = PyDict_GetItemWithError(globals, &_Py_ID(__builtins__));
    1259    15725600 :     if (builtins) {
    1260    15725500 :         if (PyModule_Check(builtins)) {
    1261       51184 :             builtins = _PyModule_GetDict(builtins);
    1262       51184 :             assert(builtins != NULL);
    1263             :         }
    1264    15725500 :         return builtins;
    1265             :     }
    1266          71 :     if (PyErr_Occurred()) {
    1267           0 :         return NULL;
    1268             :     }
    1269             : 
    1270          71 :     return _PyEval_GetBuiltins(tstate);
    1271             : }
    1272             : 
    1273             : 

Generated by: LCOV version 1.14