Modify traceback to only include the user code's execution frame
    Always call in this fashion:
        e = sys.exc_info()
        user_tb = get_user_codeframe(e[2]) or e[2]
    so that you can get the original frame back if you need to
    (this is necessary because copying traceback objects is tricky and
    this is a good workaround)

def get_user_codeframe(tb):
    """Modify traceback to only include the user code's execution frame
    Always call in this fashion:
        e = sys.exc_info()
        user_tb = get_user_codeframe(e[2]) or e[2]
    so that you can get the original frame back if you need to
    (this is necessary because copying traceback objects is tricky and
    this is a good workaround)

    """
    while tb is not None:
        f = tb.tb_frame
        co = f.f_code
        filename = co.co_filename
        if filename[0] == '<':
            # This is a meta-descriptor
            # (probably either "<unknown>" or "<string>")
            # and is likely the user's code we're executing
            return tb
        else:
            tb = tb.tb_next
    # We could not find the user's frame.
    return False