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
def _storeize(self, name):
def _storeize(self, name, func=None):
"""Return a new `name` object with an ast.Store() context"""
if not func:
func = ast.Store
if isinstance(name, Result):
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
if isinstance(name, (ast.Tuple, ast.List)):
typ = type(name)
new_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)
elif isinstance(name, ast.Name):
new_name = ast.Name(id=name.id, arg=name.arg)
@ -554,9 +557,9 @@ class HyASTCompiler(object):
elif isinstance(name, ast.Attribute):
new_name = ast.Attribute(value=name.value, attr=name.attr)
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)
return new_name