diff --git a/Lib/test/test_generated_cases.py b/Lib/test/test_generated_cases.py index 75cbd8dd94e9cb..5b78c32d641c1f 100644 --- a/Lib/test/test_generated_cases.py +++ b/Lib/test/test_generated_cases.py @@ -562,6 +562,30 @@ def test_error_if_plain_with_comment(self): """ self.run_cases_test(input, output) + def test_PyStackRef_FromPyObjectNew_with_comment(self): + input = """ + inst(OP, (-- value)) { + // Comment is ok + value = PyStackRef_FromPyObjectNew(GETITEM(FRAME_CO_CONSTS, oparg)); + } + """ + + output = """ + TARGET(OP) { + frame->instr_ptr = next_instr; + next_instr += 1; + INSTRUCTION_STATS(OP); + _PyStackRef value; + // Comment is ok + value = PyStackRef_FromPyObjectNew(GETITEM(FRAME_CO_CONSTS, oparg)); + stack_pointer[0] = value; + stack_pointer += 1; + assert(WITHIN_STACK_BOUNDS()); + DISPATCH(); + } + """ + self.run_cases_test(input, output) + def test_error_if_pop(self): input = """ inst(OP, (left, right -- res)) { diff --git a/Tools/cases_generator/analyzer.py b/Tools/cases_generator/analyzer.py index 679beca3ec3a9d..18f782e6c741ab 100644 --- a/Tools/cases_generator/analyzer.py +++ b/Tools/cases_generator/analyzer.py @@ -388,7 +388,10 @@ def find_assignment_target(node: parser.InstDef, idx: int) -> list[lexer.Token]: offset = 0 for tkn in reversed(node.block.tokens[: idx]): if tkn.kind in {"SEMI", "LBRACE", "RBRACE"}: - return node.block.tokens[idx - offset : idx] + tokens = node.block.tokens[idx - offset : idx] + while tokens and tokens[0].kind == "COMMENT": + tokens = tokens[1:] + return tokens offset += 1 return [] @@ -406,8 +409,6 @@ def find_stores_outputs(node: parser.InstDef) -> list[lexer.Token]: continue lhs = find_assignment_target(node, idx) assert lhs - while lhs and lhs[0].kind == "COMMENT": - lhs = lhs[1:] if len(lhs) != 1 or lhs[0].kind != "IDENTIFIER": continue name = lhs[0]