From ef61c524ddeeb56da3858b86e349e7288d68178e Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 24 Aug 2019 13:11:52 +0300 Subject: bpo-37830: Fix compilation of break and continue in finally. (GH-15320) Fix compilation of "break" and "continue" in the "finally" block when the corresponding "try" block contains "return" with a non-constant value. --- Objects/frameobject.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'Objects/frameobject.c') diff --git a/Objects/frameobject.c b/Objects/frameobject.c index 5deb9858ce..a796a59eee 100644 --- a/Objects/frameobject.c +++ b/Objects/frameobject.c @@ -233,6 +233,7 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignore * the 'finally' blocks. */ memset(blockstack, '\0', sizeof(blockstack)); blockstack_top = 0; + unsigned char prevop = NOP; for (addr = 0; addr < code_len; addr += sizeof(_Py_CODEUNIT)) { unsigned char op = code[addr]; switch (op) { @@ -259,17 +260,24 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignore "can't jump into the middle of a block"); return -1; } + int in_for_loop = op == FOR_ITER || code[target_addr] == END_ASYNC_FOR; if (first_in && !second_in) { - if (op != FOR_ITER && code[target_addr] != END_ASYNC_FOR) { - delta_iblock++; + if (!delta_iblock) { + if (in_for_loop) { + /* Pop the iterators of any 'for' and 'async for' loop + * we're jumping out of. */ + delta++; + } + else if (prevop == LOAD_CONST) { + /* Pops None pushed before SETUP_FINALLY. */ + delta++; + } } - else if (!delta_iblock) { - /* Pop the iterators of any 'for' and 'async for' loop - * we're jumping out of. */ - delta++; + if (!in_for_loop) { + delta_iblock++; } } - if (op != FOR_ITER && code[target_addr] != END_ASYNC_FOR) { + if (!in_for_loop) { blockstack[blockstack_top++] = target_addr; } break; @@ -293,6 +301,7 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignore break; } } + prevop = op; } /* Verify that the blockstack tracking code didn't get lost. */ -- cgit v1.2.1