Remove old deep_ast_copy code.

This commit is contained in:
Dave Halter
2015-02-12 11:24:17 +01:00
parent bcf6be0636
commit a8d3a9ab42
3 changed files with 17 additions and 57 deletions

View File

@@ -51,63 +51,19 @@ def deep_ast_copy(obj, parent=None, new_elements=None):
new_names_dict[string] = [new_elements[n] for n in names] new_names_dict[string] = [new_elements[n] for n in names]
return new_obj return new_obj
# Gather items if obj.type == 'name':
try: # Special case of a Name object.
items = list(obj.__dict__.items()) new_elements[obj] = new_obj = copy.copy(obj)
except AttributeError: if parent is not None:
# __dict__ not available, because of __slots__ new_obj.parent = parent
items = [] elif isinstance(obj, pr.BaseNode):
before = ()
for cls in obj.__class__.__mro__:
try:
if before == cls.__slots__:
continue
before = cls.__slots__
items += [(n, getattr(obj, n)) for n in before]
except AttributeError:
pass
items = sorted(items, key=lambda x: (x[0] == 'names_dict', x[0] == 'params'))
for key, value in items:
if key == 'parent':
try:
setattr(new_obj, key, new_elements[value])
except KeyError:
pass # The parent can be what it was before.
elif key == 'position_modifier':
continue
elif key == 'names_dict':
d = dict((k, sequence_recursion(v)) for k, v in value.items())
setattr(new_obj, key, d)
elif isinstance(value, (list, tuple)):
setattr(new_obj, key, sequence_recursion(value))
elif isinstance(value, (pr.BaseNode, pr.Name)):
setattr(new_obj, key, recursion(value))
return new_obj
def sequence_recursion(array_obj):
if isinstance(array_obj, tuple):
copied_array = list(array_obj)
else:
copied_array = array_obj[:] # lists, tuples, strings, unicode
for i, el in enumerate(copied_array):
if isinstance(el, (tuple, list)):
copied_array[i] = sequence_recursion(el)
else:
copied_array[i] = recursion(el)
if isinstance(array_obj, tuple):
return tuple(copied_array)
return copied_array
if parent is not None:
new_obj = copy_node(obj) new_obj = copy_node(obj)
for child in new_obj.children: if parent is not None:
if isinstance(child, (pr.Name, pr.BaseNode)): for child in new_obj.children:
child.parent = parent if isinstance(child, (pr.Name, pr.BaseNode)):
child.parent = parent
else: # String literals and so on.
new_obj = obj # Good enough, don't need to copy anything.
return new_obj return new_obj

View File

@@ -143,7 +143,7 @@ class Comprehension(IterableWrapper):
break break
last = last.children[-1] last = last.children[-1]
return helpers.deep_ast_copy(comprehension.children[0], {comprehension: last_comp}) return helpers.deep_ast_copy(comprehension.children[0], parent=last_comp)
def get_exact_index_types(self, index): def get_exact_index_types(self, index):
return [self._evaluator.eval_element(self.eval_node())[index]] return [self._evaluator.eval_element(self.eval_node())[index]]

View File

@@ -1192,6 +1192,10 @@ class CompFor(BaseNode):
arr.append(name) arr.append(name)
return dct return dct
@names_dict.setter
def names_dict(self, value):
pass
def names_dicts(self, search_global): def names_dicts(self, search_global):
yield self.names_dict yield self.names_dict