From de3b86c98d86dfbea67962f261d18908fc473e2c Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Thu, 3 Oct 2024 07:01:53 -0700 Subject: [PATCH] Insert extra braces to handle 'else if' without an 'else' correctly. --- Python/executor_cases.c.h | 22 ++++++++------- Python/generated_cases.c.h | 32 ++++++++++++---------- Python/optimizer_bytecodes.c | 16 ++++------- Python/optimizer_cases.c.h | 8 ++++-- Tools/cases_generator/generators_common.py | 4 +++ 5 files changed, 45 insertions(+), 37 deletions(-) diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h index 6cfecc0370d124..d42772e829be59 100644 --- a/Python/executor_cases.c.h +++ b/Python/executor_cases.c.h @@ -3309,18 +3309,20 @@ } iter = iterable; } - else if (PyGen_CheckExact(iterable_o)) { - iter = iterable; - } else { - /* `iterable` is not a generator. */ - _PyFrame_SetStackPointer(frame, stack_pointer); - iter = PyStackRef_FromPyObjectSteal(PyObject_GetIter(iterable_o)); - stack_pointer = _PyFrame_GetStackPointer(frame); - if (PyStackRef_IsNull(iter)) { - JUMP_TO_ERROR(); + if (PyGen_CheckExact(iterable_o)) { + iter = iterable; + } + else { + /* `iterable` is not a generator. */ + _PyFrame_SetStackPointer(frame, stack_pointer); + iter = PyStackRef_FromPyObjectSteal(PyObject_GetIter(iterable_o)); + stack_pointer = _PyFrame_GetStackPointer(frame); + if (PyStackRef_IsNull(iter)) { + JUMP_TO_ERROR(); + } + PyStackRef_CLOSE(iterable); } - PyStackRef_CLOSE(iterable); } stack_pointer[-1] = iter; break; diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 37d51ea3a78151..a9c127784edc83 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -4291,18 +4291,20 @@ } iter = iterable; } - else if (PyGen_CheckExact(iterable_o)) { - iter = iterable; - } else { - /* `iterable` is not a generator. */ - _PyFrame_SetStackPointer(frame, stack_pointer); - iter = PyStackRef_FromPyObjectSteal(PyObject_GetIter(iterable_o)); - stack_pointer = _PyFrame_GetStackPointer(frame); - if (PyStackRef_IsNull(iter)) { - goto error; + if (PyGen_CheckExact(iterable_o)) { + iter = iterable; + } + else { + /* `iterable` is not a generator. */ + _PyFrame_SetStackPointer(frame, stack_pointer); + iter = PyStackRef_FromPyObjectSteal(PyObject_GetIter(iterable_o)); + stack_pointer = _PyFrame_GetStackPointer(frame); + if (PyStackRef_IsNull(iter)) { + goto error; + } + PyStackRef_CLOSE(iterable); } - PyStackRef_CLOSE(iterable); } stack_pointer[-1] = iter; DISPATCH(); @@ -4392,11 +4394,13 @@ if (is_meth) { arg0 = PyStackRef_AsPyObjectBorrow(maybe_self[0]); } - else if (oparg) { - arg0 = PyStackRef_AsPyObjectBorrow(args[0]); - } else { - arg0 = &_PyInstrumentation_MISSING; + if (oparg) { + arg0 = PyStackRef_AsPyObjectBorrow(args[0]); + } + else { + arg0 = &_PyInstrumentation_MISSING; + } } _PyFrame_SetStackPointer(frame, stack_pointer); int err = _Py_call_instrumentation_2args( diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c index da8000864b3ae8..e476bc754341a8 100644 --- a/Python/optimizer_bytecodes.c +++ b/Python/optimizer_bytecodes.c @@ -802,11 +802,9 @@ dummy_func(void) { assert(value != NULL); eliminate_pop_guard(this_instr, !Py_IsNone(value)); } - else { - if (sym_has_type(flag)) { - assert(!sym_matches_type(flag, &_PyNone_Type)); - eliminate_pop_guard(this_instr, true); - } + else if (sym_has_type(flag)) { + assert(!sym_matches_type(flag, &_PyNone_Type)); + eliminate_pop_guard(this_instr, true); } } @@ -816,11 +814,9 @@ dummy_func(void) { assert(value != NULL); eliminate_pop_guard(this_instr, Py_IsNone(value)); } - else { - if (sym_has_type(flag)) { - assert(!sym_matches_type(flag, &_PyNone_Type)); - eliminate_pop_guard(this_instr, false); - } + else if (sym_has_type(flag)) { + assert(!sym_matches_type(flag, &_PyNone_Type)); + eliminate_pop_guard(this_instr, false); } } diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h index 66d94a6b94b24a..b08a7b89e5f91e 100644 --- a/Python/optimizer_cases.c.h +++ b/Python/optimizer_cases.c.h @@ -1843,9 +1843,11 @@ if (first_valid_check_stack == NULL) { first_valid_check_stack = corresponding_check_stack; } - else if (corresponding_check_stack) { - // delete all but the first valid _CHECK_STACK_SPACE - corresponding_check_stack->opcode = _NOP; + else { + if (corresponding_check_stack) { + // delete all but the first valid _CHECK_STACK_SPACE + corresponding_check_stack->opcode = _NOP; + } } corresponding_check_stack = NULL; break; diff --git a/Tools/cases_generator/generators_common.py b/Tools/cases_generator/generators_common.py index 6c8ea54cc54d01..f32a20b304c354 100644 --- a/Tools/cases_generator/generators_common.py +++ b/Tools/cases_generator/generators_common.py @@ -385,8 +385,12 @@ def _emit_if( self.emit(next(tkn_iter)) maybe_if = tkn_iter.peek() if maybe_if and maybe_if.kind == "IF": + #Emit extra braces around the if to get scoping right + self.emit(" {\n") self.emit(next(tkn_iter)) else_reachable, rbrace, else_storage = self._emit_if(tkn_iter, uop, storage, inst) + self.out.start_line() + self.emit("}\n") else: else_reachable, rbrace, else_storage = self._emit_block(tkn_iter, uop, storage, inst, True) if not reachable: