Skip to content
Browse files

made generic asttransformer copy better

  • Loading branch information...
1 parent 5987bd1 commit cc50e4997c01ff9c58b6260bf935bfaf85dbaef0 @cantora committed Oct 7, 2012
Showing with 14 additions and 8 deletions.
  1. +14 −8 pyc_astvisitor.py
View
22 pyc_astvisitor.py
@@ -29,7 +29,7 @@ def __init__(self):
pyc_vis.Visitor.__init__(self)
def default(self, node):
- node = copy.deepcopy(node)
+ new_node = node.__class__()
for field, old_value in ast.iter_fields(node):
old_value = getattr(node, field, None)
if isinstance(old_value, list):
@@ -43,11 +43,17 @@ def default(self, node):
new_values.extend(value)
continue
new_values.append(value)
- old_value[:] = new_values
+ setattr(new_node, field, new_values)
elif isinstance(old_value, ast.AST):
- new_node = pyc_vis.visit(self, old_value)
- if new_node is None:
- delattr(node, field)
- else:
- setattr(node, field, new_node)
- return node
+ new_child = pyc_vis.visit(self, old_value)
+ if not new_child is None:
+ setattr(new_node, field, new_child)
+
+ elif isinstance(old_value, int):
+ setattr(new_node, field, old_value)
+
+ else:
+ raise Exception("didnt expect to copy field with class %r: %r" % (old_value.__class__, old_value) )
+
+ return new_node
+

0 comments on commit cc50e49

Please sign in to comment.
Something went wrong with that request. Please try again.