Skip to content

Commit 49e1e1e

Browse files
authored
bpo-46841: Don't scan backwards in bytecode (GH-31901)
1 parent a4674f0 commit 49e1e1e

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

Objects/genobject.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,12 @@ _PyGen_yf(PyGenObject *gen)
359359
assert(code[0] != SEND);
360360
return NULL;
361361
}
362-
363-
if (code[(frame->f_lasti-1)*sizeof(_Py_CODEUNIT)] != SEND || frame->stacktop < 0)
362+
int opcode = code[(frame->f_lasti+1)*sizeof(_Py_CODEUNIT)];
363+
int oparg = code[(frame->f_lasti+1)*sizeof(_Py_CODEUNIT)+1];
364+
if (opcode != RESUME || oparg < 2) {
365+
/* Not in a yield from */
364366
return NULL;
367+
}
365368
yf = _PyFrame_StackPeek(frame);
366369
Py_INCREF(yf);
367370
}

Python/ceval.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,16 +1565,6 @@ trace_function_exit(PyThreadState *tstate, _PyInterpreterFrame *frame, PyObject
15651565
return 0;
15661566
}
15671567

1568-
static int
1569-
skip_backwards_over_extended_args(PyCodeObject *code, int offset)
1570-
{
1571-
_Py_CODEUNIT *instrs = (_Py_CODEUNIT *)PyBytes_AS_STRING(code->co_code);
1572-
while (offset > 0 && _Py_OPCODE(instrs[offset-1]) == EXTENDED_ARG) {
1573-
offset--;
1574-
}
1575-
return offset;
1576-
}
1577-
15781568
static _PyInterpreterFrame *
15791569
pop_frame(PyThreadState *tstate, _PyInterpreterFrame *frame)
15801570
{
@@ -5445,7 +5435,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
54455435
#endif
54465436
{
54475437
if (tstate->tracing == 0) {
5448-
int instr_prev = skip_backwards_over_extended_args(frame->f_code, frame->f_lasti);
5438+
int instr_prev = frame->f_lasti;
54495439
frame->f_lasti = INSTR_OFFSET();
54505440
TRACING_NEXTOPARG();
54515441
if (opcode == RESUME) {
@@ -6737,9 +6727,13 @@ maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
67376727
then call the trace function if we're tracing source lines.
67386728
*/
67396729
initialize_trace_info(&tstate->trace_info, frame);
6740-
_Py_CODEUNIT prev = ((_Py_CODEUNIT *)PyBytes_AS_STRING(frame->f_code->co_code))[instr_prev];
6730+
int entry_point = 0;
6731+
_Py_CODEUNIT *code = (_Py_CODEUNIT *)PyBytes_AS_STRING(frame->f_code->co_code);
6732+
while (_Py_OPCODE(code[entry_point]) != RESUME) {
6733+
entry_point++;
6734+
}
67416735
int lastline;
6742-
if (_Py_OPCODE(prev) == RESUME && _Py_OPARG(prev) == 0) {
6736+
if (instr_prev <= entry_point) {
67436737
lastline = -1;
67446738
}
67456739
else {

0 commit comments

Comments
 (0)