Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions parser-Python/uast/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,11 +909,18 @@ def visit_Nonlocal(self, node):
UNode.Meta()))) # todo 缺少了nonlocal的标记
return self.packPos(node, UNode.Sequence(UNode.SourceLocation(), UNode.Meta(), exprs))

def visit_withitem(self, node): # 暂不处理
def visit_withitem(self, node):
if node.optional_vars is not None:
enter = UNode.MemberAccess(UNode.SourceLocation(), UNode.Meta(),
self.packPos(node.context_expr, self.visit(node.context_expr)),
UNode.Identifier(UNode.SourceLocation(), UNode.Meta(), "__enter__"))

call = UNode.CallExpression(UNode.SourceLocation(), UNode.Meta(),
self.packPos(node.context_expr, enter), [])

return UNode.VariableDeclaration(UNode.SourceLocation(), UNode.Meta(),
self.packPos(node.optional_vars, self.visit(node.optional_vars)),
self.packPos(node.context_expr, self.visit(node.context_expr)), False,
self.packPos(node.context_expr, call), False,
UNode.DynamicType(UNode.SourceLocation(), UNode.Meta()))
else:
return UNode.Noop(UNode.SourceLocation(), UNode.Meta())
Comment on lines +912 to 926

Choose a reason for hiding this comment

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

high

The current implementation correctly handles with <expr> as <var>: by generating a call to __enter__. However, it doesn't handle with <expr>: (without an as clause). In this case, __enter__ should still be called, but its return value is discarded. The current code returns a Noop, which is incorrect.

I've provided a suggestion that fixes this by moving the __enter__ call logic outside the if condition. The else branch now wraps this call in an ExpressionStatement to indicate it's called for its side effects. This change also improves readability by breaking down the complex node creation and location packing.

Suggested change
def visit_withitem(self, node):
if node.optional_vars is not None:
enter = UNode.MemberAccess(UNode.SourceLocation(), UNode.Meta(),
self.packPos(node.context_expr, self.visit(node.context_expr)),
UNode.Identifier(UNode.SourceLocation(), UNode.Meta(), "__enter__"))
call = UNode.CallExpression(UNode.SourceLocation(), UNode.Meta(),
self.packPos(node.context_expr, enter), [])
return UNode.VariableDeclaration(UNode.SourceLocation(), UNode.Meta(),
self.packPos(node.optional_vars, self.visit(node.optional_vars)),
self.packPos(node.context_expr, self.visit(node.context_expr)), False,
self.packPos(node.context_expr, call), False,
UNode.DynamicType(UNode.SourceLocation(), UNode.Meta()))
else:
return UNode.Noop(UNode.SourceLocation(), UNode.Meta())
def visit_withitem(self, node):
context_uast = self.packPos(node.context_expr, self.visit(node.context_expr))
enter_access = UNode.MemberAccess(UNode.SourceLocation(), UNode.Meta(),
context_uast,
UNode.Identifier(UNode.SourceLocation(), UNode.Meta(), "__enter__"))
self.packPos(node.context_expr, enter_access)
enter_call = UNode.CallExpression(UNode.SourceLocation(), UNode.Meta(), enter_access, [])
self.packPos(node.context_expr, enter_call)
if node.optional_vars is not None:
return UNode.VariableDeclaration(UNode.SourceLocation(), UNode.Meta(),
self.packPos(node.optional_vars, self.visit(node.optional_vars)),
enter_call, False,
UNode.DynamicType(UNode.SourceLocation(), UNode.Meta()))
else:
return self.packPos(node, UNode.ExpressionStatement(UNode.SourceLocation(), UNode.Meta(), enter_call))

Expand Down