def copy_stack_trace(from_var, to_var):
    """
    Copies the stack trace from one or more tensor variables to
    one or more tensor variables and returns the destination variables.

    Parameters
    ----------
    from_var
        Tensor variable or list of tensor variables to copy stack traces from.
    to_var
        Tensor variable or list of tensor variables to copy stack traces to.

    Notes
    -----
    The stacktrace is assumed to be of the form of a list of lists
    of tuples. Each tuple contains the filename, line number, function name
    and so on. Each list of tuples contains the truples belonging to a
    particular variable.

    """
    tr = []
    if type(from_var) is list:
        for v in from_var:
            tr += getattr(v.tag, 'trace', [])
    else:
        tr = getattr(from_var.tag, 'trace', [])
    if tr and isinstance(tr[0], tuple):
        tr = [tr]
    if type(to_var) is list:
        for v in to_var:
            v.tag.trace = getattr(v.tag, 'trace', []) + tr
    else:
        to_var.tag.trace = getattr(to_var.tag, 'trace', []) + tr
    return to_var
