def frames_filter(frames):
    """
    Filter any frames out which are not interesting according to the pytest/paste rules.

    The variables we look for are '__traceback_hide__' or '__tracebackhide__' defined in local scope.

    Allows the stack frame, or those related to it, to be hidden from the traceback
    if they are not directly the cause of the exception.

    Values:
        - True
            The frame will be hidden.
        - 'before'
            All frames before this will be hidden
        - 'before_and_this'
            All frames before this will be hidden, and this frame too
        - 'after'
            All frames after this will be hidden (up to a 'reset' value)
        - 'after_and_this'
            All frames after this will be hidden, and this frame too (up to a 'reset' value)
        - 'reset'
            Terminates any 'after' hiding.
    """
    new_frames_list = []
    ignore_subsequent_frames = False
    if len(frames) == 0:
        return frames
    last_frame = frames[-1]
    for frame in frames:
        tb_frame = getattr(frame, 'tb_frame', None)
        f_locals = getattr(tb_frame, 'f_locals', {})
        hide = f_locals.get('__traceback_hide__', None)
        if hide is None:
            hide = f_locals.get('__tracebackhide__', None)
        if hide is not None:
            if hide == 'reset':
                ignore_subsequent_frames = False
            elif hide == 'after':
                ignore_subsequent_frames = True
                if frame != last_frame:
                    new_frames_list.append(frame)
            elif hide == 'after_and_this':
                ignore_subsequent_frames = True
            elif hide == 'before':
                if frame != last_frame:
                    new_frames_list = []
            elif hide == 'before_and_this':
                if frame != last_frame:
                    new_frames_list = []
                    continue
            elif hide == True:
                if frame != last_frame:
                    continue
        if not ignore_subsequent_frames:
            new_frames_list.append(frame)
    if not new_frames_list:
        new_frames_list = [last_frame]
    return new_frames_list
