Skip to content

Conversation

markshannon
Copy link
Member

@markshannon markshannon commented Jul 31, 2025

Copy link
Member

@brandtbucher brandtbucher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems correct, nice fix.

Comment on lines 829 to 882
def op_escapes(op: parser.CodeDef) -> bool:

def is_simple_exit(stmt: Stmt) -> bool:
if not isinstance(stmt, SimpleStmt):
return False
tokens = stmt.contents
if len(tokens) < 4:
return False
return (
tokens[0].text in ("ERROR_IF", "DEOPT_IF", "EXIT_IF")
and
tokens[1].text == "("
and
tokens[2].text in ("true", "1")
and
tokens[3].text == ")"
)

def escapes_list(stmts: list[Stmt]) -> bool:
if not stmts:
return False
if is_simple_exit(stmts[-1]):
return False
for stmt in stmts:
if escapes(stmt):
return True
return False

def escapes(stmt: Stmt) -> bool:
if isinstance(stmt, BlockStmt):
return escapes_list(stmt.body)
elif isinstance(stmt, SimpleStmt):
for tkn in stmt.contents:
if tkn.text == "DECREF_INPUTS":
return True
d: dict[SimpleStmt, EscapingCall] = {}
escaping_call_in_simple_stmt(stmt, d)
return bool(d)
elif isinstance(stmt, IfStmt):
if stmt.else_body and escapes(stmt.else_body):
return True
return escapes(stmt.body)
elif isinstance(stmt, MacroIfStmt):
if stmt.else_body and escapes_list(stmt.else_body):
return True
return escapes_list(stmt.body)
elif isinstance(stmt, ForStmt):
return escapes(stmt.body)
elif isinstance(stmt, WhileStmt):
return escapes(stmt.body)
else:
assert False, "Unexpected statement type"

return escapes(op.block)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way these functions are nested is sort of weird. Is there a reason they're not just defined at the top level?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just grouping. I've flattened the hierarchy to be more consistent with the rest of the code.

@markshannon markshannon merged commit 801cf3f into python:main Aug 2, 2025
48 checks passed
@markshannon markshannon deleted the exits-arent-escapes branch August 2, 2025 15:51
Agent-Hellboy pushed a commit to Agent-Hellboy/cpython that referenced this pull request Aug 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants