Skip to content

Commit

Permalink
Check that lhs of assignment or deleted expression is valid.
Browse files Browse the repository at this point in the history
whitequark committed May 8, 2015
1 parent 0266a52 commit f0ddee7
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions pyparser/parser.py
Original file line number Diff line number Diff line change
@@ -397,9 +397,22 @@ def _wrap_tuple(self, elts):
else:
return elts[0]

def _assignable(self, node):
# TODO
return node
def _assignable(self, node, is_delete=False):
if isinstance(node, ast.Name) or isinstance(node, ast.Subscript) or \
isinstance(node, ast.Attribute):
return node
elif (isinstance(node, ast.List) or isinstance(node, ast.Tuple)) and \
any(node.elts):
node.elts = [self._assignable(elt, is_delete) for elt in node.elts]
return node
else:
if is_delete:
error = diagnostic.Diagnostic(
"error", "cannot delete this expression", {}, node.loc)
else:
error = diagnostic.Diagnostic(
"error", "cannot assign to this expression", {}, node.loc)
raise diagnostic.DiagnosticException(error)

def _empty_arguments(self):
return ast.arguments(args=[], defaults=[], vararg=None, kwarg=None,
@@ -635,7 +648,7 @@ def del_stmt(self, stmt_loc, exprs):
# Python uses exprlist here, but does *not* obey the usual
# tuple-wrapping semantics, so we embed the rule directly.
"""del_stmt: 'del' exprlist"""
return ast.Delete(targets=list(map(self._assignable, exprs)),
return ast.Delete(targets=[self._assignable(expr, is_delete=True) for expr in exprs],
loc=stmt_loc.join(exprs[-1].loc), keyword_loc=stmt_loc)

@action(Loc('pass'))

0 comments on commit f0ddee7

Please sign in to comment.