def replace_print(pos, opts, old_node=None): """ Replace old_node with a new statement. Also hacks in the "end" functionality. """ new_node = new_print(*pos, **opts) end = None if "end" not in opts else opts["end"].clone() file = None if "file" not in opts else opts["file"].clone() if old_node is None: parent = Node(syms.simple_stmt, [Leaf(token.NEWLINE, "\n")]) i = 0 else: parent = old_node.parent i = old_node.remove() parent.insert_child(i, new_node) if end is not None and not (end.type == token.STRING and \ end.value in ("'\\n'", '"\\n"')): add_end_part(end, file, parent, i + 1) return new_node