Index: Lib/lib2to3/pytree.py =================================================================== --- Lib/lib2to3/pytree.py (Revision 74653) +++ Lib/lib2to3/pytree.py (Arbeitskopie) @@ -130,6 +130,8 @@ def replace(self, new): """Replace this node with a new one in the parent.""" + if self is new: + return assert self.parent is not None, str(self) assert new is not None if not isinstance(new, list): @@ -147,9 +149,12 @@ assert found, (self.children, self, new) self.parent.changed() self.parent.children = l_children + # one of the new nodes may be self; + # make sure it doesn't lose its parent. + parent = self.parent + self.parent = None for x in new: - x.parent = self.parent - self.parent = None + x.parent = parent def get_lineno(self): """Return the line number which generated the invocant node."""