Implement libinternals
A bunch of functions from `lib` that are only used in core.internals, have light dependencies.

Small optimizations: made `slice_getitem` a `cdef` instead of `def`, use PySlice_Check instead of isinstance.