import copy
import ast


def ast_expr_for_pudb(p, name='_val'):
    """
    Transforms ast p into a suitable ast for PuDB.

    Converts any ending expr into an assignment, so that smart_eval()
    evaluates the whole thing with a single exec().

    The final expression, if any, will be assigned to `name`.
    """
    p = copy.deepcopy(p)
    if p.body and isinstance(p.body[-1], ast.Expr):
        expr = p.body.pop()

        a = ast.copy_location(ast.Assign(targets=[ast.Name(id=name, ctx=ast.Store())],
            value=expr.value), expr)
        p.body.append(a)

        ast.fix_missing_locations(p)
    return p