(original) (raw)
Line
Count
Source (jump to first uncovered line)
/* Frame object implementation */
#include "Python.h"
#include "pycore_ceval.h" // _PyEval_BuiltinsFromGlobals()
#include "pycore_code.h" // CO_FAST_LOCAL, etc.
#include "pycore_function.h" // _PyFunction_FromConstructor()
#include "pycore_moduleobject.h" // _PyModule_GetDict()
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
#include "pycore_opcode.h" // _PyOpcode_Caches
#include "frameobject.h" // PyFrameObject
#include "pycore_frame.h"
#include "opcode.h" // EXTENDED_ARG
#include "structmember.h" // PyMemberDef
#define OFF(x) offsetof(PyFrameObject, x)
static PyMemberDef frame_memberlist[] = {
{"f_trace_lines", T_BOOL, OFF(f_trace_lines), 0},
{"f_trace_opcodes", T_BOOL, OFF(f_trace_opcodes), 0},
{NULL} /* Sentinel */
};
static PyObject *
frame_getlocals(PyFrameObject *f, void *closure)
{
if (PyFrame_FastToLocalsWithError(f) < 0)
Branch (28:9): [True: 0, False: 448]
return NULL;
PyObject *locals = f->f_frame->f_locals;
Py_INCREF(locals);
return locals;
}
int
PyFrame_GetLineNumber(PyFrameObject *f)
{
assert(f != NULL);
if (f->f_lineno != 0) {
Branch (39:9): [True: 574k, False: 4.03M]
return f->f_lineno;
}
else {
return _PyInterpreterFrame_GetLine(f->f_frame);
}
}
static PyObject *
frame_getlineno(PyFrameObject *f, void *closure)
{
int lineno = PyFrame_GetLineNumber(f);
if (lineno < 0) {
Branch (51:9): [True: 103, False: 703k]
Py_RETURN_NONE;
}
else {
return PyLong_FromLong(lineno);
}
}
static PyObject *
frame_getlasti(PyFrameObject *f, void *closure)
{
int lasti = _PyInterpreterFrame_LASTI(f->f_frame);
if (lasti < 0) {
Branch (63:9): [True: 0, False: 582]
return PyLong_FromLong(-1);
}
return PyLong_FromLong(lasti * sizeof(_Py_CODEUNIT));
}
static PyObject *
frame_getglobals(PyFrameObject *f, void *closure)
{
PyObject *globals = f->f_frame->f_globals;
if (globals == NULL) {
Branch (73:9): [True: 0, False: 241k]
globals = Py_None;
}
Py_INCREF(globals);
return globals;
}
static PyObject *
frame_getbuiltins(PyFrameObject *f, void *closure)
{
PyObject *builtins = f->f_frame->f_builtins;
if (builtins == NULL) {
Branch (84:9): [True: 0, False: 3]
builtins = Py_None;
}
Py_INCREF(builtins);
return builtins;
}
static PyObject *
frame_getcode(PyFrameObject *f, void *closure)
{
if (PySys_Audit("object.__getattr__", "Os", f, "f_code") < 0) {
Branch (94:9): [True: 0, False: 846k]
return NULL;
}
return (PyObject *)PyFrame_GetCode(f);
}
static PyObject *
frame_getback(PyFrameObject *f, void *closure)
{
PyObject *res = (PyObject *)PyFrame_GetBack(f);
if (res == NULL) {
Branch (104:9): [True: 2.45k, False: 132k]
Py_RETURN_NONE;
}
return res;
}
// Given the index of the effective opcode, scan back to construct the oparg
// with EXTENDED_ARG. This only works correctly with unquickened code,
// obtained via a call to _PyCode_GetCode!
static unsigned int
get_arg(const _Py_CODEUNIT *codestr, Py_ssize_t i)
{
_Py_CODEUNIT word;
unsigned int oparg = _Py_OPARG(codestr[i]);
if (i >= 1 && _Py_OPCODE(word = codestr[i-1]) == EXTENDED_ARG) {
Branch (118:9): [True: 402, False: 0] Branch (118:19): [True: 0, False: 402]
oparg |= _Py_OPARG(word) << 8;
if (i >= 2 && _Py_OPCODE(word = codestr[i-2]) == EXTENDED_ARG) {
Branch (120:13): [True: 0, False: 0] Branch (120:23): [True: 0, False: 0]
oparg |= _Py_OPARG(word) << 16;
if (i >= 3 && _Py_OPCODE(word = codestr[i-3]) == EXTENDED_ARG) {
Branch (122:17): [True: 0, False: 0] Branch (122:27): [True: 0, False: 0]
oparg |= _Py_OPARG(word) << 24;
}
}
}
return oparg;
}
/* Model the evaluation stack, to determine which jumps
- are safe and how many values needs to be popped.
- The stack is modelled by a 64 integer, treating any
- stack that can't fit into 64 bits as "overflowed".
*/
typedef enum kind {
Iterator = 1,
Except = 2,
Object = 3,
Null = 4,
} Kind;
static int
compatible_kind(Kind from, Kind to) {
if (to == 0) {
Branch (145:9): [True: 0, False: 11]
return 0;
}
if (to == Object) {
Branch (148:9): [True: 3, False: 8]
return from != Null;
}
if (to == Null) {
Branch (151:9): [True: 4, False: 4]
return 1;
}
return from == to;
}
#define BITS_PER_BLOCK 3
#define UNINITIALIZED -2
#define OVERFLOWED -1
#define MAX_STACK_ENTRIES (63/BITS_PER_BLOCK)
#define WILL_OVERFLOW (1ULL<<((MAX_STACK_ENTRIES-1)*BITS_PER_BLOCK))
static inline int64_t
push_value(int64_t stack, Kind kind)
{
if (((uint64_t)stack) >= WILL_OVERFLOW) {
Branch (168:9): [True: 0, False: 1.49k]
return OVERFLOWED;
}
else {
return (stack << BITS_PER_BLOCK) | kind;
}
}
static inline int64_t
pop_value(int64_t stack)
{
return Py_ARITHMETIC_RIGHT_SHIFT(int64_t, stack, BITS_PER_BLOCK);
}
static inline Kind
top_of_stack(int64_t stack)
{
return stack & ((1<<BITS_PER_BLOCK)-1);
}
static int64_t *
mark_stacks(PyCodeObject *code_obj, int len)
{
PyObject *co_code = _PyCode_GetCode(code_obj);
if (co_code == NULL) {
Branch (192:9): [True: 0, False: 88]
return NULL;
}
_Py_CODEUNIT *code = (_Py_CODEUNIT *)PyBytes_AS_STRING(co_code);
int64_t *stacks = PyMem_New(int64_t, len+1);
int i, j, opcode;
if (stacks == NULL) {
Branch (199:9): [True: 0, False: 88]
PyErr_NoMemory();
Py_DECREF(co_code);
return NULL;
}
for (int i = 1; 88
i <= len;
i++7.96k
) {
Branch (204:21): [True: 7.96k, False: 88]
stacks[i] = UNINITIALIZED;
}
stacks[0] = 0;
if (code_obj->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR))
Branch (208:9): [True: 20, False: 68]
{
// Generators get sent None while starting:
stacks[0] = push_value(stacks[0], Object);
}
int todo = 1;
while (todo) {
Branch (214:12): [True: 88, False: 88]
todo = 0;
for (i = 0; i < len;
i++7.96k
) {
Branch (216:21): [True: 7.96k, False: 88]
int64_t next_stack = stacks[i];
if (next_stack == UNINITIALIZED) {
Branch (218:17): [True: 1.97k, False: 5.99k]
continue;
}
opcode = _Py_OPCODE(code[i]);
switch (opcode) {
case JUMP_IF_FALSE_OR_POP:
Branch (223:17): [True: 0, False: 5.99k]
case JUMP_IF_TRUE_OR_POP:
Branch (224:17): [True: 0, False: 5.99k]
case POP_JUMP_FORWARD_IF_FALSE:
Branch (225:17): [True: 4, False: 5.99k]
case POP_JUMP_BACKWARD_IF_FALSE:
Branch (226:17): [True: 0, False: 5.99k]
case POP_JUMP_FORWARD_IF_TRUE:
Branch (227:17): [True: 6, False: 5.98k]
case POP_JUMP_BACKWARD_IF_TRUE:
Branch (228:17): [True: 2, False: 5.99k]
{
int64_t target_stack;
int j = get_arg(code, i);
if (opcode == POP_JUMP_FORWARD_IF_FALSE ||
Branch (232:25): [True: 4, False: 8]
opcode == 8
POP_JUMP_FORWARD_IF_TRUE8
) {
Branch (233:25): [True: 6, False: 2]
j += i + 1;
}
else if (opcode == POP_JUMP_BACKWARD_IF_FALSE ||
Branch (236:30): [True: 0, False: 2]
opcode == POP_JUMP_BACKWARD_IF_TRUE) {
Branch (237:30): [True: 2, False: 0]
j = i + 1 - j;
}
assert(j < len);
if (stacks[j] == UNINITIALIZED &&
j < i10
) {
Branch (241:25): [True: 10, False: 2] Branch (241:55): [True: 0, False: 10]
todo = 1;
}
if (opcode == JUMP_IF_FALSE_OR_POP ||
Branch (244:25): [True: 0, False: 12]
opcode == JUMP_IF_TRUE_OR_POP)
Branch (245:25): [True: 0, False: 12]
{
target_stack = next_stack;
next_stack = pop_value(next_stack);
}
else {
next_stack = pop_value(next_stack);
target_stack = next_stack;
}
assert(stacks[j] == UNINITIALIZED || stacks[j] == target_stack);
stacks[j] = target_stack;
stacks[i+1] = next_stack;
break;
}
case SEND:
Branch (259:17): [True: 33, False: 5.96k]
j = get_arg(code, i) + i + 1;
assert(j < len);
assert(stacks[j] == UNINITIALIZED || stacks[j] == pop_value(next_stack));
stacks[j] = pop_value(next_stack);
stacks[i+1] = next_stack;
break;
case JUMP_FORWARD:
Branch (266:17): [True: 3, False: 5.99k]
j = get_arg(code, i) + i + 1;
assert(j < len);
assert(stacks[j] == UNINITIALIZED || stacks[j] == next_stack);
stacks[j] = next_stack;
break;
case JUMP_BACKWARD:
Branch (272:17): [True: 22, False: 5.97k]
case JUMP_BACKWARD_NO_INTERRUPT:
Branch (273:17): [True: 33, False: 5.96k]
j = i + 1 - get_arg(code, i);
assert(j >= 0);
assert(j < len);
if (stacks[j] == UNINITIALIZED &&
j < i0
) {
Branch (277:25): [True: 0, False: 55] Branch (277:55): [True: 0, False: 0]
todo = 1;
}
assert(stacks[j] == UNINITIALIZED || stacks[j] == next_stack);
stacks[j] = next_stack;
break;
case GET_ITER:
Branch (283:17): [True: 21, False: 5.97k]
case GET_AITER:
Branch (284:17): [True: 9, False: 5.98k]
next_stack = push_value(pop_value(next_stack), Iterator);
stacks[i+1] = next_stack;
break;
case FOR_ITER:
Branch (288:17): [True: 18, False: 5.97k]
{
int64_t target_stack = pop_value(next_stack);
stacks[i+1] = push_value(next_stack, Object);
j = get_arg(code, i) + 1 + INLINE_CACHE_ENTRIES_FOR_ITER + i;
assert(j < len);
assert(stacks[j] == UNINITIALIZED || stacks[j] == target_stack);
stacks[j] = target_stack;
break;
}
case END_ASYNC_FOR:
Branch (298:17): [True: 0, False: 5.99k]
next_stack = pop_value(pop_value(pop_value(next_stack)));
stacks[i+1] = next_stack;
break;
case PUSH_EXC_INFO:
Branch (302:17): [True: 0, False: 5.99k]
case POP_EXCEPT:
Branch (303:17): [True: 0, False: 5.99k]
/* These instructions only appear in exception handlers, which
* skip this switch ever since the move to zero-cost exceptions
* (their stack remains UNINITIALIZED because nothing sets it).
*
* Note that explain_incompatible_stack interprets an
* UNINITIALIZED stack as belonging to an exception handler.
*/
Py_UNREACHABLE();
break;
case RETURN_VALUE:
Branch (313:17): [True: 90, False: 5.90k]
case RAISE_VARARGS:
Branch (314:17): [True: 0, False: 5.99k]
case RERAISE:
Branch (315:17): [True: 0, False: 5.99k]
/* End of block */
break;
case PUSH_NULL:
Branch (318:17): [True: 4, False: 5.99k]
next_stack = push_value(next_stack, Null);
stacks[i+1] = next_stack;
break;
case LOAD_GLOBAL:
Branch (322:17): [True: 54, False: 5.94k]
{
int j = get_arg(code, i);
if (j & 1) {
Branch (325:25): [True: 51, False: 3]
next_stack = push_value(next_stack, Null);
}
next_stack = push_value(next_stack, Object);
stacks[i+1] = next_stack;
break;
}
case LOAD_ATTR:
Branch (332:17): [True: 227, False: 5.76k]
{
int j = get_arg(code, i);
if (j & 1) {
Branch (335:25): [True: 227, False: 0]
next_stack = pop_value(next_stack);
next_stack = push_value(next_stack, Null);
next_stack = push_value(next_stack, Object);
}
stacks[i+1] = next_stack;
break;
}
default:
Branch (343:17): [True: 5.46k, False: 526]
{
int delta = PyCompile_OpcodeStackEffect(opcode, _Py_OPARG(code[i]));
while (delta < 0) {
Branch (346:28): [True: 1.08k, False: 5.46k]
next_stack = pop_value(next_stack);
delta++;
}
while (delta > 0) {
Branch (350:28): [True: 868, False: 5.46k]
next_stack = push_value(next_stack, Object);
delta--;
}
stacks[i+1] = next_stack;
}
}
}
}
Py_DECREF(co_code);
return stacks;
}
static int
compatible_stack(int64_t from_stack, int64_t to_stack)
{
if (from_stack < 0 ||
to_stack < 091
) {
Branch (366:9): [True: 10, False: 91] Branch (366:27): [True: 19, False: 72]
return 0;
}
while(72
from_stack > to_stack) {
Branch (369:11): [True: 30, False: 72]
from_stack = pop_value(from_stack);
}
while(from_stack) {
Branch (372:11): [True: 11, False: 69]
Kind from_top = top_of_stack(from_stack);
Kind to_top = top_of_stack(to_stack);
if (!compatible_kind(from_top, to_top)) {
Branch (375:13): [True: 3, False: 8]
return 0;
}
from_stack = pop_value(from_stack);
to_stack = pop_value(to_stack);
}
return to_stack == 0;
}
static const char *
explain_incompatible_stack(int64_t to_stack)
{
assert(to_stack != 0);
if (to_stack == OVERFLOWED) {
Branch (388:9): [True: 0, False: 22]
return "stack is too deep to analyze";
}
if (to_stack == UNINITIALIZED) {
Branch (391:9): [True: 10, False: 12]
return "can't jump into an exception handler, or code may be unreachable";
}
Kind target_kind = top_of_stack(to_stack);
switch(target_kind) {
case Except:
Branch (396:9): [True: 0, False: 12]
return "can't jump into an 'except' block as there's no exception";
case Object:
Branch (398:9): [True: 3, False: 9]
case Null:
Branch (399:9): [True: 4, False: 8]
return "incompatible stacks";
case Iterator:
Branch (401:9): [True: 5, False: 7]
return "can't jump into the body of a for loop";
default:
Branch (403:9): [True: 0, False: 12]
Py_UNREACHABLE();
}
}
static int *
marklines(PyCodeObject *code, int len)
{
PyCodeAddressRange bounds;
_PyCode_InitAddressRange(code, &bounds);
assert (bounds.ar_end == 0);
int last_line = -1;
int *linestarts = PyMem_New(int, len);
if (linestarts == NULL) {
Branch (417:9): [True: 0, False: 91]
return NULL;
}
for (int i = 0; 91
i < len;
i++8.14k
) {
Branch (420:21): [True: 8.14k, False: 91]
linestarts[i] = -1;
}
while (_PyLineTable_NextAddressRange(&bounds)) {
Branch (424:12): [True: 3.82k, False: 91]
assert(bounds.ar_start / (int)sizeof(_Py_CODEUNIT) < len);
if (bounds.ar_line != last_line &&
bounds.ar_line != -1942
) {
Branch (426:13): [True: 942, False: 2.88k] Branch (426:44): [True: 692, False: 250]
linestarts[bounds.ar_start / sizeof(_Py_CODEUNIT)] = bounds.ar_line;
last_line = bounds.ar_line;
}
}
return linestarts;
}
static int
first_line_not_before(int *lines, int len, int line)
{
int result = INT_MAX;
for (int i = 0; i < len;
i++8.14k
) {
Branch (438:21): [True: 8.14k, False: 91]
if (lines[i] < result &&
lines[i] >= line7.97k
) {
Branch (439:13): [True: 7.97k, False: 172] Branch (439:34): [True: 98, False: 7.87k]
result = lines[i];
}
}
if (result == INT_MAX) {
Branch (443:9): [True: 3, False: 88]
return -1;
}
return result;
}
static void
frame_stack_pop(PyFrameObject *f)
{
PyObject *v = _PyFrame_StackPop(f->f_frame);
Py_XDECREF(v);
}
static PyFrameState
_PyFrame_GetState(PyFrameObject *frame)
{
if (frame->f_frame->stacktop == 0) {
Branch (459:9): [True: 50, False: 220]
return FRAME_CLEARED;
}
switch(frame->f_frame->owner) {
Branch (462:12): [True: 0, False: 220]
case FRAME_OWNED_BY_GENERATOR:
Branch (463:9): [True: 45, False: 175]
{
PyGenObject *gen = _PyFrame_GetGenerator(frame->f_frame);
return gen->gi_frame_state;
}
case FRAME_OWNED_BY_THREAD:
Branch (468:9): [True: 175, False: 45]
{
if (_PyInterpreterFrame_LASTI(frame->f_frame) < 0) {
Branch (470:17): [True: 0, False: 175]
return FRAME_CREATED;
}
switch (_PyOpcode_Deopt[_Py_OPCODE(*frame->f_frame->prev_instr)])
{
case COPY_FREE_VARS:
Branch (475:17): [True: 0, False: 175]
case MAKE_CELL:
Branch (476:17): [True: 0, False: 175]
case RETURN_GENERATOR:
Branch (477:17): [True: 0, False: 175]
/* Frame not fully initialized */
return FRAME_CREATED;
default:
Branch (480:17): [True: 175, False: 0]
return FRAME_EXECUTING;
}
}
case FRAME_OWNED_BY_FRAME_OBJECT:
Branch (484:9): [True: 0, False: 220]
return FRAME_COMPLETED;
}
Py_UNREACHABLE0
();
}
static void
add_load_fast_null_checks(PyCodeObject *co)
{
int changed = 0;
_Py_CODEUNIT *instructions = _PyCode_CODE(co);
for (Py_ssize_t i = 0; i < Py_SIZE(co);
i++9.37k
) {
Branch (495:28): [True: 9.37k, False: 94]
switch (_Py_OPCODE(instructions[i])) {
case LOAD_FAST:
Branch (497:13): [True: 376, False: 9.00k]
case LOAD_FAST__LOAD_FAST:
Branch (498:13): [True: 3, False: 9.37k]
case LOAD_FAST__LOAD_CONST:
Branch (499:13): [True: 1, False: 9.37k]
changed = 1;
_Py_SET_OPCODE(instructions[i], LOAD_FAST_CHECK);
break;
case LOAD_CONST__LOAD_FAST:
Branch (503:13): [True: 4, False: 9.37k]
changed = 1;
_Py_SET_OPCODE(instructions[i], LOAD_CONST);
break;
case STORE_FAST__LOAD_FAST:
Branch (507:13): [True: 2, False: 9.37k]
changed = 1;
_Py_SET_OPCODE(instructions[i], STORE_FAST);
break;
}
}
if (changed) {
Branch (513:9): [True: 86, False: 8]
// invalidate cached co_code object
Py_CLEAR(co->_co_code);
}
}
/* Setter for f_lineno - you can set f_lineno from within a trace function in
- order to jump to a given line of code, subject to some restrictions. Most
- lines are OK to jump to because they don't make any assumptions about the
- state of the stack (obvious because you could remove the line and the code
- would still work without any stack errors), but there are some constructs
- that limit jumping:
- o Any exception handlers.
- o 'for' and 'async for' loops can't be jumped into because the
- iterator needs to be on the stack.
- o Jumps cannot be made from within a trace function invoked with a
- 'return' or 'exception' event since the eval loop has been exited at
- that time.
*/
static int
frame_setlineno(PyFrameObject f, PyObject p_new_lineno, void *Py_UNUSED(ignored))
{
if (p_new_lineno == NULL) {
Branch (536:9): [True: 1, False: 97]
PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
return -1;
}
/* f_lineno must be an integer. */
if (!PyLong_CheckExact(p_new_lineno)) {
Branch (541:9): [True: 1, False: 96]
PyErr_SetString(PyExc_ValueError,
"lineno must be an integer");
return -1;
}
PyFrameState state = _PyFrame_GetState(f);
/*
* This code preserves the historical restrictions on
* setting the line number of a frame.
* Jumps are forbidden on a 'return' trace event (except after a yield).
* Jumps from 'call' trace events are also forbidden.
* In addition, jumps are forbidden when not tracing,
* as this is a debugging feature.
*/
switch(PyThreadState_GET()->tracing_what) {
case PyTrace_EXCEPTION:
Branch (557:9): [True: 1, False: 95]
PyErr_SetString(PyExc_ValueError,
"can only jump from a 'line' trace event");
return -1;
case PyTrace_CALL:
Branch (561:9): [True: 2, False: 94]
PyErr_Format(PyExc_ValueError,
"can't jump from the 'call' trace event of a new frame");
return -1;
case PyTrace_LINE:
Branch (565:9): [True: 91, False: 5]
break;
case PyTrace_RETURN:
Branch (567:9): [True: 2, False: 94]
if (state == FRAME_SUSPENDED) {
Branch (568:17): [True: 1, False: 1]
break;
}
/* fall through */
default:
Branch (572:9): [True: 0, False: 96]
PyErr_SetString(PyExc_ValueError,
"can only jump from a 'line' trace event");
return -1;
}
if (!f->f_trace) {
Branch (577:9): [True: 0, False: 92]
PyErr_Format(PyExc_ValueError,
"f_lineno can only be set by a trace function");
return -1;
}
int new_lineno;
/* Fail if the line falls outside the code block and
select first line with actual code. */
int overflow;
long l_new_lineno = PyLong_AsLongAndOverflow(p_new_lineno, &overflow);
if (overflow
Branch (589:9): [True: 0, False: 92]
#if SIZEOF_LONG > SIZEOF_INT
|| l_new_lineno > INT_MAX
Branch (591:12): [True: 0, False: 92]
|| l_new_lineno < INT_MIN
Branch (592:12): [True: 0, False: 92]
#endif
) {
PyErr_SetString(PyExc_ValueError,
"lineno out of range");
return -1;
}
new_lineno = (int)l_new_lineno;
if (new_lineno < f->f_frame->f_code->co_firstlineno) {
Branch (601:9): [True: 1, False: 91]
PyErr_Format(PyExc_ValueError,
"line %d comes before the current code block",
new_lineno);
return -1;
}
add_load_fast_null_checks(f->f_frame->f_code);
/* PyCode_NewWithPosOnlyArgs limits co_code to be under INT_MAX so this
* should never overflow. */
int len = (int)Py_SIZE(f->f_frame->f_code);
int *lines = marklines(f->f_frame->f_code, len);
if (lines == NULL) {
Branch (614:9): [True: 0, False: 91]
return -1;
}
new_lineno = first_line_not_before(lines, len, new_lineno);
if (new_lineno < 0) {
Branch (619:9): [True: 3, False: 88]
PyErr_Format(PyExc_ValueError,
"line %d comes after the current code block",
(int)l_new_lineno);
PyMem_Free(lines);
return -1;
}
int64_t *stacks = mark_stacks(f->f_frame->f_code, len);
if (stacks == NULL) {
Branch (628:9): [True: 0, False: 88]
PyMem_Free(lines);
return -1;
}
int64_t best_stack = OVERFLOWED;
int best_addr = -1;
int64_t start_stack = stacks[_PyInterpreterFrame_LASTI(f->f_frame)];
int err = -1;
const char *msg = "cannot find bytecode for specified line";
for (int i = 0; i < len;
i++7.96k
) {
Branch (638:21): [True: 7.96k, False: 88]
if (lines[i] == new_lineno) {
Branch (639:13): [True: 101, False: 7.86k]
int64_t target_stack = stacks[i];
if (compatible_stack(start_stack, target_stack)) {
Branch (641:17): [True: 58, False: 43]
err = 0;
if (target_stack > best_stack) {
Branch (643:21): [True: 58, False: 0]
best_stack = target_stack;
best_addr = i;
}
}
else if (err < 0) {
Branch (648:22): [True: 32, False: 11]
if (start_stack == OVERFLOWED) {
Branch (649:21): [True: 0, False: 32]
msg = "stack to deep to analyze";
}
else if (start_stack == UNINITIALIZED) {
Branch (652:26): [True: 10, False: 22]
msg = "can't jump from within an exception handler";
}
else {
msg = explain_incompatible_stack(target_stack);
err = 1;
}
}
}
}
PyMem_Free(stacks);
PyMem_Free(lines);
if (err) {
Branch (664:9): [True: 30, False: 58]
PyErr_SetString(PyExc_ValueError, msg);
return -1;
}
if (state == FRAME_SUSPENDED) {
Branch (668:9): [True: 1, False: 57]
/* Account for value popped by yield */
start_stack = pop_value(start_stack);
}
while (start_stack > best_stack) {
Branch (672:12): [True: 26, False: 58]
frame_stack_pop(f);
start_stack = pop_value(start_stack);
}
/* Finally set the new lasti and return OK. */
f->f_lineno = 0;
f->f_frame->prev_instr = _PyCode_CODE(f->f_frame->f_code) + best_addr;
return 0;
}
static PyObject *
frame_gettrace(PyFrameObject *f, void *closure)
{
PyObject* trace = f->f_trace;
if (trace == NULL)
Branch (687:9): [True: 2, False: 5]
trace = Py_None;
Py_INCREF(trace);
return trace;
}
static int
frame_settrace(PyFrameObject f, PyObject v, void *closure)
{
if (v == Py_None) {
Branch (698:9): [True: 3, False: 1.62k]
v = NULL;
}
Py_XINCREF(v);
Py_XSETREF(f->f_trace, v);
return 0;
}
static PyGetSetDef frame_getsetlist[] = {
{"f_back", (getter)frame_getback, NULL, NULL},
{"f_locals", (getter)frame_getlocals, NULL, NULL},
{"f_lineno", (getter)frame_getlineno,
(setter)frame_setlineno, NULL},
{"f_trace", (getter)frame_gettrace, (setter)frame_settrace, NULL},
{"f_lasti", (getter)frame_getlasti, NULL, NULL},
{"f_globals", (getter)frame_getglobals, NULL, NULL},
{"f_builtins", (getter)frame_getbuiltins, NULL, NULL},
{"f_code", (getter)frame_getcode, NULL, NULL},
{0}
};
/* Stack frames are allocated and deallocated at a considerable rate.
In an attempt to improve the speed of function calls, we maintain
a separate free list of stack frames (just like floats are
allocated in a special way -- see floatobject.c). When a stack
frame is on the free list, only the following members have a meaning:
ob_type == &Frametype
f_back next item on free list, or NULL
*/
static void
frame_dealloc(PyFrameObject *f)
{
/* It is the responsibility of the owning generator/coroutine
* to have cleared the generator pointer */
assert(f->f_frame->owner != FRAME_OWNED_BY_GENERATOR ||
_PyFrame_GetGenerator(f->f_frame)->gi_frame_state == FRAME_CLEARED);
if (_PyObject_GC_IS_TRACKED(f)) {
_PyObject_GC_UNTRACK(f);
}
Py_TRASHCAN_BEGIN(f, frame_dealloc);
PyCodeObject *co = NULL;
/* Kill all local variables including specials, if we own them */
if (f->f_frame->owner == FRAME_OWNED_BY_FRAME_OBJECT) {
Branch (747:9): [True: 1.12M, False: 2.33M]
assert(f->f_frame == (_PyInterpreterFrame *)f->_f_frame_data);
_PyInterpreterFrame *frame = (_PyInterpreterFrame *)f->_f_frame_data;
/* Don't clear code object until the end */
co = frame->f_code;
frame->f_code = NULL;
Py_CLEAR(frame->f_func);
Py_CLEAR(frame->f_locals);
PyObject **locals = _PyFrame_GetLocalsArray(frame);
for (int i = 0; i < frame->stacktop;
i++6.19M
) {
Branch (756:25): [True: 6.19M, False: 1.12M]
Py_CLEAR(locals[i]);
}
}
Py_CLEAR(f->f_back);
Py_CLEAR(f->f_trace);
PyObject_GC_Del(f);
Py_XDECREF(co);
Py_TRASHCAN_END;
}
static int
frame_traverse(PyFrameObject *f, visitproc visit, void *arg)
{
Py_VISIT(f->f_back);
Py_VISIT(f->f_trace);
if (f->f_frame->owner != FRAME_OWNED_BY_FRAME_OBJECT) {
Branch (772:9): [True: 0, False: 1.52M]
return 0;
}
assert(f->f_frame->frame_obj == NULL);
return _PyFrame_Traverse(f->f_frame, visit, arg);
}
static int
frame_tp_clear(PyFrameObject *f)
{
Py_CLEAR(f->f_trace);
/* locals and stack */
PyObject **locals = _PyFrame_GetLocalsArray(f->f_frame);
assert(f->f_frame->stacktop >= 0);
for (int i = 0; i < f->f_frame->stacktop;
i++536k
) {
Branch (787:21): [True: 536k, False: 70.2k]
Py_CLEAR(locals[i]);
}
f->f_frame->stacktop = 0;
return 0;
}
static PyObject *
frame_clear(PyFrameObject *f, PyObject *Py_UNUSED(ignored))
{
if (f->f_frame->owner == FRAME_OWNED_BY_GENERATOR) {
Branch (797:9): [True: 723, False: 255k]
PyGenObject *gen = _PyFrame_GetGenerator(f->f_frame);
if (gen->gi_frame_state == FRAME_EXECUTING) {
Branch (799:13): [True: 720, False: 3]
goto running;
}
_PyGen_Finalize((PyObject *)gen);
}
else if (f->f_frame->owner == FRAME_OWNED_BY_THREAD) {
Branch (804:14): [True: 185k, False: 69.6k]
goto running;
}
else {
assert(f->f_frame->owner == FRAME_OWNED_BY_FRAME_OBJECT);
(void)frame_tp_clear(f);
}
Py_RETURN_NONE69.6k
;
running:
PyErr_SetString(PyExc_RuntimeError,
"cannot clear an executing frame");
return NULL;
}
PyDoc_STRVAR(clear__doc__,
"F.clear(): clear most references held by the frame");
static PyObject *
frame_sizeof(PyFrameObject *f, PyObject *Py_UNUSED(ignored))
{
Py_ssize_t res;
res = offsetof(PyFrameObject, _f_frame_data) + offsetof(_PyInterpreterFrame, localsplus);
PyCodeObject *code = f->f_frame->f_code;
res += (code->co_nlocalsplus+code->co_stacksize) * sizeof(PyObject *);
return PyLong_FromSsize_t(res);
}
PyDoc_STRVAR(sizeof__doc__,
"F.sizeof() -> size of F in memory, in bytes");
static PyObject *
frame_repr(PyFrameObject *f)
{
int lineno = PyFrame_GetLineNumber(f);
PyCodeObject *code = f->f_frame->f_code;
return PyUnicode_FromFormat(
"<frame at %p, file %R, line %d, code %S>",
f, code->co_filename, lineno, code->co_name);
}
static PyMethodDef frame_methods[] = {
{"clear", (PyCFunction)frame_clear, METH_NOARGS,
clear__doc__},
{"__sizeof__", (PyCFunction)frame_sizeof, METH_NOARGS,
sizeof__doc__},
{NULL, NULL} /* sentinel */
};
PyTypeObject PyFrame_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"frame",
offsetof(PyFrameObject, _f_frame_data) +
offsetof(_PyInterpreterFrame, localsplus),
sizeof(PyObject *),
(destructor)frame_dealloc, /* tp_dealloc */
0, /* tp_vectorcall_offset */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_as_async */
(reprfunc)frame_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
PyObject_GenericSetAttr, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
0, /* tp_doc */
(traverseproc)frame_traverse, /* tp_traverse */
(inquiry)frame_tp_clear, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
frame_methods, /* tp_methods */
frame_memberlist, /* tp_members */
frame_getsetlist, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
};
static void
init_frame(_PyInterpreterFrame *frame, PyFunctionObject *func, PyObject *locals)
{
/* _PyFrame_InitializeSpecials consumes reference to func */
Py_INCREF(func);
Py_XINCREF(locals);
PyCodeObject *code = (PyCodeObject *)func->func_code;
_PyFrame_InitializeSpecials(frame, func, locals, code);
for (Py_ssize_t i = 0; i < code->co_nlocalsplus;
i++0
) {
Branch (896:28): [True: 0, False: 30]
frame->localsplus[i] = NULL;
}
}
PyFrameObject*
_PyFrame_New_NoTrack(PyCodeObject *code)
{
CALL_STAT_INC(frame_objects_created);
int slots = code->co_nlocalsplus + code->co_stacksize;
PyFrameObject *f = PyObject_GC_NewVar(PyFrameObject, &PyFrame_Type, slots);
if (f == NULL) {
Branch (907:9): [True: 0, False: 3.46M]
return NULL;
}
f->f_back = NULL;
f->f_trace = NULL;
f->f_trace_lines = 1;
f->f_trace_opcodes = 0;
f->f_fast_as_locals = 0;
f->f_lineno = 0;
return f;
}
/* Legacy API */
PyFrameObject*
PyFrame_New(PyThreadState *tstate, PyCodeObject *code,
PyObject *globals, PyObject *locals)
{
PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
if (builtins == NULL) {
Branch (925:9): [True: 0, False: 30]
return NULL;
}
PyFrameConstructor desc = {
.fc_globals = globals,
.fc_builtins = builtins,
.fc_name = code->co_name,
.fc_qualname = code->co_name,
.fc_code = (PyObject *)code,
.fc_defaults = NULL,
.fc_kwdefaults = NULL,
.fc_closure = NULL
};
PyFunctionObject *func = _PyFunction_FromConstructor(&desc);
if (func == NULL) {
Branch (939:9): [True: 0, False: 30]
return NULL;
}
PyFrameObject *f = _PyFrame_New_NoTrack(code);
if (f == NULL) {
Branch (943:9): [True: 0, False: 30]
Py_DECREF(func);
return NULL;
}
init_frame((_PyInterpreterFrame *)f->_f_frame_data, func, locals);
f->f_frame = (_PyInterpreterFrame *)f->_f_frame_data;
f->f_frame->owner = FRAME_OWNED_BY_FRAME_OBJECT;
Py_DECREF(func);
_PyObject_GC_TRACK(f);
return f;
}
static int
_PyFrame_OpAlreadyRan(_PyInterpreterFrame *frame, int opcode, int oparg)
{
// This only works when opcode is a non-quickened form:
assert(_PyOpcode_Deopt[opcode] == opcode);
int check_oparg = 0;
for (_Py_CODEUNIT *instruction = _PyCode_CODE(frame->f_code);
instruction < frame->prev_instr;
instruction++2.00k
)
Branch (962:10): [True: 7.03k, False: 0]
{
int check_opcode = _PyOpcode_Deopt[_Py_OPCODE(*instruction)];
check_oparg |= _Py_OPARG(*instruction);
if (check_opcode == opcode &&
check_oparg == oparg7.03k
) {
Branch (966:13): [True: 7.03k, False: 1] Branch (966:39): [True: 5.03k, False: 2.00k]
return 1;
}
if (check_opcode == EXTENDED_ARG) {
Branch (969:13): [True: 0, False: 2.00k]
check_oparg <<= 8;
}
else {
check_oparg = 0;
}
instruction += _PyOpcode_Caches[check_opcode];
}
return 0;
}
int
_PyFrame_FastToLocalsWithError(_PyInterpreterFrame *frame) {
/* Merge fast locals into f->f_locals */
PyObject *locals;
PyObject **fast;
PyCodeObject *co;
locals = frame->f_locals;
if (locals == NULL) {
Branch (987:9): [True: 4.50k, False: 9.96k]
locals = frame->f_locals = PyDict_New();
if (locals == NULL)
Branch (989:13): [True: 0, False: 4.50k]
return -1;
}
co = frame->f_code;
fast = _PyFrame_GetLocalsArray(frame);
// COPY_FREE_VARS has no quickened forms, so no need to use _PyOpcode_Deopt
// here:
int lasti = _PyInterpreterFrame_LASTI(frame);
if (lasti < 0 &&
_Py_OPCODE0
(_PyCode_CODE(co)[0]) == 0
COPY_FREE_VARS0
) {
Branch (997:9): [True: 0, False: 14.4k] Branch (997:22): [True: 0, False: 0]
/* Free vars have not been initialized -- Do that */
PyCodeObject *co = frame->f_code;
PyObject *closure = frame->f_func->func_closure;
int offset = co->co_nlocals + co->co_nplaincellvars;
for (int i = 0; i < co->co_nfreevars; ++i) {
Branch (1002:25): [True: 0, False: 0]
PyObject *o = PyTuple_GET_ITEM(closure, i);
Py_INCREF(o);
frame->localsplus[offset + i] = o;
}
// COPY_FREE_VARS doesn't have inline CACHEs, either:
frame->prev_instr = _PyCode_CODE(frame->f_code);
}
for (int i = 0; i < co->co_nlocalsplus;
i++99.1k
) {
Branch (1010:21): [True: 99.1k, False: 14.4k]
_PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i);
/* If the namespace is unoptimized, then one of the
following cases applies:
1. It does not contain free variables, because it
uses import * or is a top-level namespace.
2. It is a class namespace.
We don't want to accidentally copy free variables
into the locals dict used by the class.
*/
if (kind & CO_FAST_FREE &&
!(co->co_flags & 254
CO_OPTIMIZED254
)) {
Branch (1021:13): [True: 254, False: 98.8k] Branch (1021:36): [True: 5, False: 249]
continue;
}
PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
PyObject *value = fast[i];
if (frame->stacktop) {
Branch (1027:13): [True: 99.0k, False: 17]
if (kind & CO_FAST_FREE) {
Branch (1028:17): [True: 247, False: 98.8k]
// The cell was set by COPY_FREE_VARS.
assert(value != NULL && PyCell_Check(value));
value = PyCell_GET(value);
}
else if (kind & CO_FAST_CELL) {
Branch (1033:22): [True: 5.02k, False: 93.8k]
// Note that no *_DEREF ops can happen before MAKE_CELL
// executes. So there's no need to duplicate the work
// that MAKE_CELL would otherwise do later, if it hasn't
// run yet.
if (value != NULL) {
Branch (1038:21): [True: 5.02k, False: 0]
if (PyCell_Check(value) &&
_PyFrame_OpAlreadyRan(frame, MAKE_CELL, i)) {
Branch (1040:29): [True: 5.02k, False: 0]
// (likely) MAKE_CELL must have executed already.
value = PyCell_GET(value);
}
// (likely) Otherwise it it is an arg (kind & CO_FAST_LOCAL),
// with the initial value set when the frame was created...
// (unlikely) ...or it was set to some initial value by
// an earlier call to PyFrame_LocalsToFast().
}
}
}
else {
assert(value == NULL);
}
if (value == NULL) {
Branch (1054:13): [True: 10.7k, False: 88.3k]
if (PyObject_DelItem(locals, name) != 0) {
Branch (1055:17): [True: 10.7k, False: 6]
if (PyErr_ExceptionMatches(PyExc_KeyError)) {
Branch (1056:21): [True: 10.7k, False: 0]
PyErr_Clear();
}
else {
return -1;
}
}
}
else {
if (PyObject_SetItem(locals, name, value) != 0) {
Branch (1065:17): [True: 0, False: 88.3k]
return -1;
}
}
}
return 0;
}
int
PyFrame_FastToLocalsWithError(PyFrameObject *f)
{
if (f == NULL) {
Branch (1076:9): [True: 0, False: 489]
PyErr_BadInternalCall();
return -1;
}
int err = _PyFrame_FastToLocalsWithError(f->f_frame);
if (err == 0) {
Branch (1081:9): [True: 489, False: 0]
f->f_fast_as_locals = 1;
}
return err;
}
void
PyFrame_FastToLocals(PyFrameObject *f)
{
int res;
assert(!PyErr_Occurred());
res = PyFrame_FastToLocalsWithError(f);
if (res < 0)
Branch (1095:9): [True: 0, False: 0]
PyErr_Clear();
}
void
_PyFrame_LocalsToFast(_PyInterpreterFrame *frame, int clear)
{
/* Merge locals into fast locals */
PyObject *locals;
PyObject **fast;
PyObject *error_type, *error_value, *error_traceback;
PyCodeObject *co;
locals = frame->f_locals;
if (locals == NULL) {
Branch (1108:9): [True: 0, False: 2.00k]
return;
}
fast = _PyFrame_GetLocalsArray(frame);
co = frame->f_code;
bool added_null_checks = false;
PyErr_Fetch(&error_type, &error_value, &error_traceback);
for (int i = 0; i < co->co_nlocalsplus;
i++276
) {
Branch (1116:21): [True: 276, False: 2.00k]
_PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i);
/* Same test as in PyFrame_FastToLocals() above. */
if (kind & CO_FAST_FREE &&
!(co->co_flags & 6
CO_OPTIMIZED6
)) {
Branch (1120:13): [True: 6, False: 270] Branch (1120:36): [True: 0, False: 6]
continue;
}
PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
PyObject *value = PyObject_GetItem(locals, name);
/* We only care about NULLs if clear is true. */
if (value == NULL) {
Branch (1126:13): [True: 57, False: 219]
PyErr_Clear();
if (!clear) {
Branch (1128:17): [True: 0, False: 57]
continue;
}
}
PyObject *oldvalue = fast[i];
if (!added_null_checks &&
oldvalue != NULL272
&&
value == NULL218
) {
Branch (1133:13): [True: 272, False: 4] Branch (1133:35): [True: 218, False: 54] Branch (1133:55): [True: 3, False: 215]
add_load_fast_null_checks(co);
added_null_checks = true;
}
PyObject *cell = NULL;
if (kind == CO_FAST_FREE) {
Branch (1138:13): [True: 6, False: 270]
// The cell was set when the frame was created from
// the function's closure.
assert(oldvalue != NULL && PyCell_Check(oldvalue));
cell = oldvalue;
}
else if (kind & CO_FAST_CELL &&
oldvalue != NULL6
) {
Branch (1144:18): [True: 6, False: 264] Branch (1144:41): [True: 6, False: 0]
/* Same test as in PyFrame_FastToLocals() above. */
if (PyCell_Check(oldvalue) &&
_PyFrame_OpAlreadyRan(frame, MAKE_CELL, i)) {
Branch (1147:21): [True: 6, False: 0]
// (likely) MAKE_CELL must have executed already.
cell = oldvalue;
}
// (unlikely) Otherwise, it must have been set to some
// initial value by an earlier call to PyFrame_LocalsToFast().
}
if (cell != NULL) {
Branch (1154:13): [True: 12, False: 264]
oldvalue = PyCell_GET(cell);
if (value != oldvalue) {
Branch (1156:17): [True: 0, False: 12]
Py_XINCREF(value);
PyCell_SET(cell, value);
Py_XDECREF(oldvalue);
}
}
else if (value != oldvalue) {
Branch (1162:18): [True: 3, False: 261]
Py_XINCREF(value);
Py_XSETREF(fast[i], value);
}
Py_XDECREF(value);
}
PyErr_Restore(error_type, error_value, error_traceback);
}
void
PyFrame_LocalsToFast(PyFrameObject *f, int clear)
{
if (f && f->f_fast_as_locals &&
_PyFrame_GetState(f) != FRAME_CLEARED174
) {
Branch (1174:9): [True: 889k, False: 0] Branch (1174:14): [True: 174, False: 889k] Branch (1174:37): [True: 125, False: 49]
_PyFrame_LocalsToFast(f->f_frame, clear);
f->f_fast_as_locals = 0;
}
}
int _PyFrame_IsEntryFrame(PyFrameObject *frame)
{
assert(frame != NULL);
return frame->f_frame->is_entry;
}
PyCodeObject *
PyFrame_GetCode(PyFrameObject *frame)
{
assert(frame != NULL);
PyCodeObject *code = frame->f_frame->f_code;
assert(code != NULL);
Py_INCREF(code);
return code;
}
PyFrameObject*
PyFrame_GetBack(PyFrameObject *frame)
{
assert(frame != NULL);
PyFrameObject *back = frame->f_back;
if (back == NULL) {
Branch (1204:9): [True: 137k, False: 36]
_PyInterpreterFrame *prev = frame->f_frame->previous;
while (prev &&
_PyFrame_IsIncomplete(prev)135k
) {
Branch (1206:16): [True: 135k, False: 2.45k] Branch (1206:24): [True: 4, False: 135k]
prev = prev->previous;
}
if (prev) {
Branch (1209:13): [True: 135k, False: 2.45k]
back = _PyFrame_GetFrameObject(prev);
}
}
Py_XINCREF(back);
return back;
}
PyObject*
PyFrame_GetLocals(PyFrameObject *frame)
{
return frame_getlocals(frame, NULL);
}
PyObject*
PyFrame_GetGlobals(PyFrameObject *frame)
{
return frame_getglobals(frame, NULL);
}
PyObject*
PyFrame_GetBuiltins(PyFrameObject *frame)
{
return frame_getbuiltins(frame, NULL);
}
int
PyFrame_GetLasti(PyFrameObject *frame)
{
int lasti = _PyInterpreterFrame_LASTI(frame->f_frame);
if (lasti < 0) {
Branch (1239:9): [True: 0, False: 1]
return -1;
}
return lasti * sizeof(_Py_CODEUNIT);
}
PyObject *
PyFrame_GetGenerator(PyFrameObject *frame)
{
if (frame->f_frame->owner != FRAME_OWNED_BY_GENERATOR) {
Branch (1248:9): [True: 0, False: 1]
return NULL;
}
PyGenObject *gen = _PyFrame_GetGenerator(frame->f_frame);
return Py_NewRef(gen);
}
PyObject*
_PyEval_BuiltinsFromGlobals(PyThreadState *tstate, PyObject *globals)
{
PyObject *builtins = PyDict_GetItemWithError(globals, &_Py_ID(__builtins__));
if (builtins) {
Branch (1259:9): [True: 2.57M, False: 71]
if (PyModule_Check(builtins)) {
builtins = _PyModule_GetDict(builtins);
assert(builtins != NULL);
}
return builtins;
}
if (PyErr_Occurred()) {
Branch (1266:9): [True: 0, False: 71]
return NULL;
}
return _PyEval_GetBuiltins(tstate);
}