def _slice_defaults(s, length):
    """ apply slice defaulting conventions """
    assert(length >= 0)

    step = 1 if s.step is None else s.step

    if s.start is not None:
        start = s.start
        if start < 0:
            start += length
    else:
        start = 0 if step > 0 else (length - 1)

    if s.stop is not None:
        stop = s.stop
        if stop < 0:
            stop += length
    else:
        stop = length if step > 0 else -length - 1

    return slice(start, stop, step)