Skip to content

Commit 821759d

Browse files
authored
gh-126211: Exclude preprocessor directives from statements containing escaping calls (#126213)
The cases generator inserts code to save and restore the stack pointer around statements that contain escaping calls. To find the beginning of such statements, we would walk backwards from the escaping call until we encountered a token that was treated as a statement terminator. This set of terminators should include preprocessor directives.
1 parent 32e07fd commit 821759d

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

Lib/test/test_generated_cases.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,6 +1429,39 @@ def test_instruction_size_macro(self):
14291429
with self.assertRaisesRegex(SyntaxError, "All instructions containing a uop"):
14301430
self.run_cases_test(input, output)
14311431

1432+
def test_escaping_call_next_to_cmacro(self):
1433+
input = """
1434+
inst(OP, (--)) {
1435+
#ifdef Py_GIL_DISABLED
1436+
escaping_call();
1437+
#else
1438+
another_escaping_call();
1439+
#endif
1440+
yet_another_escaping_call();
1441+
}
1442+
"""
1443+
output = """
1444+
TARGET(OP) {
1445+
frame->instr_ptr = next_instr;
1446+
next_instr += 1;
1447+
INSTRUCTION_STATS(OP);
1448+
#ifdef Py_GIL_DISABLED
1449+
_PyFrame_SetStackPointer(frame, stack_pointer);
1450+
escaping_call();
1451+
stack_pointer = _PyFrame_GetStackPointer(frame);
1452+
#else
1453+
_PyFrame_SetStackPointer(frame, stack_pointer);
1454+
another_escaping_call();
1455+
stack_pointer = _PyFrame_GetStackPointer(frame);
1456+
#endif
1457+
_PyFrame_SetStackPointer(frame, stack_pointer);
1458+
yet_another_escaping_call();
1459+
stack_pointer = _PyFrame_GetStackPointer(frame);
1460+
DISPATCH();
1461+
}
1462+
"""
1463+
self.run_cases_test(input, output)
1464+
14321465

14331466
class TestGeneratedAbstractCases(unittest.TestCase):
14341467
def setUp(self) -> None:

Tools/cases_generator/analyzer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ def find_stmt_start(node: parser.InstDef, idx: int) -> lexer.Token:
637637
assert idx < len(node.block.tokens)
638638
while True:
639639
tkn = node.block.tokens[idx-1]
640-
if tkn.kind in {"SEMI", "LBRACE", "RBRACE"}:
640+
if tkn.kind in {"SEMI", "LBRACE", "RBRACE", "CMACRO"}:
641641
break
642642
idx -= 1
643643
assert idx > 0

0 commit comments

Comments
 (0)