Skip to content

Commit 39376cb

Browse files
authored
gh-108991: replace _PyFrame_GetState by two simpler functions (#108992)
1 parent 5f3433f commit 39376cb

File tree

1 file changed

+20
-31
lines changed

1 file changed

+20
-31
lines changed

Objects/frameobject.c

+20-31
Original file line numberDiff line numberDiff line change
@@ -591,39 +591,28 @@ first_line_not_before(int *lines, int len, int line)
591591
return result;
592592
}
593593

594-
static PyFrameState
595-
_PyFrame_GetState(PyFrameObject *frame)
594+
static bool
595+
frame_is_cleared(PyFrameObject *frame)
596596
{
597597
assert(!_PyFrame_IsIncomplete(frame->f_frame));
598598
if (frame->f_frame->stacktop == 0) {
599-
return FRAME_CLEARED;
599+
return true;
600600
}
601-
switch(frame->f_frame->owner) {
602-
case FRAME_OWNED_BY_GENERATOR:
603-
{
604-
PyGenObject *gen = _PyFrame_GetGenerator(frame->f_frame);
605-
return gen->gi_frame_state;
606-
}
607-
case FRAME_OWNED_BY_THREAD:
608-
{
609-
if (_PyInterpreterFrame_LASTI(frame->f_frame) < 0) {
610-
return FRAME_CREATED;
611-
}
612-
switch (frame->f_frame->prev_instr->op.code)
613-
{
614-
case COPY_FREE_VARS:
615-
case MAKE_CELL:
616-
case RETURN_GENERATOR:
617-
/* Frame not fully initialized */
618-
return FRAME_CREATED;
619-
default:
620-
return FRAME_EXECUTING;
621-
}
622-
}
623-
case FRAME_OWNED_BY_FRAME_OBJECT:
624-
return FRAME_COMPLETED;
601+
if (frame->f_frame->owner == FRAME_OWNED_BY_GENERATOR) {
602+
PyGenObject *gen = _PyFrame_GetGenerator(frame->f_frame);
603+
return gen->gi_frame_state == FRAME_CLEARED;
604+
}
605+
return false;
606+
}
607+
608+
static bool frame_is_suspended(PyFrameObject *frame)
609+
{
610+
assert(!_PyFrame_IsIncomplete(frame->f_frame));
611+
if (frame->f_frame->owner == FRAME_OWNED_BY_GENERATOR) {
612+
PyGenObject *gen = _PyFrame_GetGenerator(frame->f_frame);
613+
return gen->gi_frame_state == FRAME_SUSPENDED;
625614
}
626-
Py_UNREACHABLE();
615+
return false;
627616
}
628617

629618
/* Setter for f_lineno - you can set f_lineno from within a trace function in
@@ -655,7 +644,7 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignore
655644
return -1;
656645
}
657646

658-
PyFrameState state = _PyFrame_GetState(f);
647+
bool is_suspended = frame_is_suspended(f);
659648
/*
660649
* This code preserves the historical restrictions on
661650
* setting the line number of a frame.
@@ -811,7 +800,7 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignore
811800
}
812801
assert(unbound == 0);
813802
}
814-
if (state == FRAME_SUSPENDED) {
803+
if (is_suspended) {
815804
/* Account for value popped by yield */
816805
start_stack = pop_value(start_stack);
817806
}
@@ -1455,7 +1444,7 @@ void
14551444
PyFrame_LocalsToFast(PyFrameObject *f, int clear)
14561445
{
14571446
assert(!_PyFrame_IsIncomplete(f->f_frame));
1458-
if (f && f->f_fast_as_locals && _PyFrame_GetState(f) != FRAME_CLEARED) {
1447+
if (f && f->f_fast_as_locals && !frame_is_cleared(f)) {
14591448
_PyFrame_LocalsToFast(f->f_frame, clear);
14601449
f->f_fast_as_locals = 0;
14611450
}

0 commit comments

Comments
 (0)