{-# LINE 1 "lib/CPython/Types/Slice.chs" #-}
{-# LANGUAGE ForeignFunctionInterface #-}
module CPython.Types.Slice
( Slice
, sliceType
, new
, getIndices
) where
import Prelude hiding (length)
import CPython.Internal hiding (new)
newtype Slice = Slice (ForeignPtr Slice)
instance Object Slice where
toObject (Slice x) = SomeObject x
fromForeignPtr = Slice
instance Concrete Slice where
concreteType _ = sliceType
sliceType :: (Type)
sliceType =
unsafePerformIO $
let {res = sliceType'_} in
peekStaticObject res >>= \res' ->
return (res')
{-# LINE 41 "lib/CPython/Types/Slice.chs" #-}
new :: (Object start, Object stop, Object step) => Maybe start -> Maybe stop -> Maybe step -> IO Slice
new start stop step =
maybeWith withObject start $ \startPtr ->
maybeWith withObject stop $ \stopPtr ->
maybeWith withObject step $ \stepPtr ->
pySliceNew startPtr stopPtr stepPtr
>>= stealObject
getIndices :: Slice
-> Integer
-> IO (Integer, Integer, Integer, Integer)
getIndices slice length =
withObject slice $ \slicePtr ->
let length' = fromIntegral length in
alloca $ \startPtr ->
alloca $ \stopPtr ->
alloca $ \stepPtr ->
alloca $ \sliceLenPtr -> do
pySliceGetIndicesEx
{-# LINE 67 "lib/CPython/Types/Slice.chs" #-}
slicePtr length' startPtr stopPtr stepPtr sliceLenPtr
>>= checkStatusCode
start <- fmap toInteger $ peek startPtr
stop <- fmap toInteger $ peek stopPtr
step <- fmap toInteger $ peek stepPtr
sliceLen <- fmap toInteger $ peek sliceLenPtr
return (start, stop, step, sliceLen)
foreign import ccall unsafe "CPython/Types/Slice.chs.h hscpython_PySlice_Type"
sliceType'_ :: (Ptr ())
foreign import ccall safe "CPython/Types/Slice.chs.h PySlice_New"
pySliceNew :: ((Ptr ()) -> ((Ptr ()) -> ((Ptr ()) -> (IO (Ptr ())))))
foreign import ccall safe "CPython/Types/Slice.chs.h PySlice_GetIndicesEx"
pySliceGetIndicesEx :: ((Ptr ()) -> (CLong -> ((Ptr CLong) -> ((Ptr CLong) -> ((Ptr CLong) -> ((Ptr CLong) -> (IO CInt)))))))