Make _storeize generic

This allows to change an expression's context to something else than ast.Store if needed.
This commit is contained in:
Nicolas Dandrimont 2013-12-21 23:33:14 +01:00 committed by Berker Peksag
parent cccf90c702
commit 0f96c24965

View File

@ -534,18 +534,21 @@ class HyASTCompiler(object):
return ret, args, defaults, varargs, kwargs return ret, args, defaults, varargs, kwargs
def _storeize(self, name): def _storeize(self, name, func=None):
"""Return a new `name` object with an ast.Store() context""" """Return a new `name` object with an ast.Store() context"""
if not func:
func = ast.Store
if isinstance(name, Result): if isinstance(name, Result):
if not name.is_expr(): if not name.is_expr():
raise TypeError("Can't assign to a non-expr") raise TypeError("Can't assign / delete a non-expression")
name = name.expr name = name.expr
if isinstance(name, (ast.Tuple, ast.List)): if isinstance(name, (ast.Tuple, ast.List)):
typ = type(name) typ = type(name)
new_elts = [] new_elts = []
for x in name.elts: for x in name.elts:
new_elts.append(self._storeize(x)) new_elts.append(self._storeize(x, func))
new_name = typ(elts=new_elts) new_name = typ(elts=new_elts)
elif isinstance(name, ast.Name): elif isinstance(name, ast.Name):
new_name = ast.Name(id=name.id, arg=name.arg) new_name = ast.Name(id=name.id, arg=name.arg)
@ -554,9 +557,9 @@ class HyASTCompiler(object):
elif isinstance(name, ast.Attribute): elif isinstance(name, ast.Attribute):
new_name = ast.Attribute(value=name.value, attr=name.attr) new_name = ast.Attribute(value=name.value, attr=name.attr)
else: else:
raise TypeError("Can't assign to a %s object" % type(name)) raise TypeError("Can't assign / delete a %s object" % type(name))
new_name.ctx = ast.Store() new_name.ctx = func()
ast.copy_location(new_name, name) ast.copy_location(new_name, name)
return new_name return new_name