Python: Add with

Co-authored-by: yoff <yoff@github.com>
This commit is contained in:
Taus
2026-04-21 15:05:48 +00:00
committed by yoff
parent 0acbb12fb9
commit b229066891

View File

@@ -228,6 +228,19 @@ private module Ast {
ExprNode getCause() { result.asExpr() = raise.getCause() }
}
/** A `with` statement. */
class WithNode extends StmtNode {
private Py::With withStmt;
WithNode() { withStmt = this.asStmt() }
ExprNode getContextExpr() { result.asExpr() = withStmt.getContextExpr() }
ExprNode getOptionalVars() { result.asExpr() = withStmt.getOptionalVars() }
StmtListNode getBody() { result.asStmtList() = withStmt.getBody() }
}
/** A `break` statement. */
class BreakNode extends StmtNode {
BreakNode() { this.asStmt() instanceof Py::Break }
@@ -650,6 +663,15 @@ module AstSigImpl implements AstSig<Py::Location> {
// Delete: targets left to right
result = n.(Ast::DeleteNode).getTarget(index)
or
// With: context expr (0), optional vars (1), body (2)
exists(Ast::WithNode w | w = n |
index = 0 and result = w.getContextExpr()
or
index = 1 and result = w.getOptionalVars()
or
index = 2 and result = w.getBody()
)
or
// ThrowStmt (raise): the exception (0), the cause (1)
exists(Ast::RaiseNode r | r = n |
index = 0 and result = r.getException()