module CPython.Types.List
( List
, listType
, toList
, iterableToList
, fromList
, length
, getItem
, setItem
, insert
, append
, getSlice
, setSlice
, sort
, reverse
, toTuple
) where
import Prelude hiding (reverse, length)
import CPython.Internal hiding (new)
import qualified CPython.Types.Tuple as T
instance Concrete List where
concreteType _ = listType
listType :: Type
listType =
unsafePerformIO $
let {res = listType'_} in
peekStaticObject res >>= \res' ->
return (res')
toList :: [SomeObject] -> IO List
toList xs =
mapWith withObject xs $ \ptrs ->
withArrayLen ptrs $ \count array ->
hscpython_poke_list (fromIntegral count) array
>>= stealObject
iterableToList :: Object iter => iter -> IO List
iterableToList iter = do
raw <- callObjectRaw listType =<< T.toTuple [toObject iter]
return $ unsafeCast raw
fromList :: List -> IO [SomeObject]
fromList py =
withObject py $ \pyPtr ->
(pyListSize pyPtr >>=) $ \size ->
let size' = fromIntegral size :: Int in
withArray (replicate size' nullPtr) $ \ptrs ->
hscpython_peek_list pyPtr size ptrs >>
peekArray size' ptrs >>= mapM peekObject
length :: List -> IO (Integer)
length a1 =
withObject a1 $ \a1' ->
length'_ a1' >>= \res ->
checkIntReturn res >>= \res' ->
return (res')
getItem :: List -> Integer -> IO (SomeObject)
getItem a1 a2 =
withObject a1 $ \a1' ->
let {a2' = fromIntegral a2} in
getItem'_ a1' a2' >>= \res ->
peekObject res >>= \res' ->
return (res')
setItem :: Object o => List -> Integer -> o -> IO ()
setItem self index x =
withObject self $ \selfPtr ->
withObject x $ \xPtr -> do
incref xPtr
pyListSetItem selfPtr (fromIntegral index) xPtr
>>= checkStatusCode
insert :: Object item => List -> Integer -> item -> IO (())
insert a1 a2 a3 =
withObject a1 $ \a1' ->
let {a2' = fromIntegral a2} in
withObject a3 $ \a3' ->
insert'_ a1' a2' a3' >>= \res ->
checkStatusCode res >>= \res' ->
return (res')
append :: Object item => List -> item -> IO (())
append a1 a2 =
withObject a1 $ \a1' ->
withObject a2 $ \a2' ->
append'_ a1' a2' >>= \res ->
checkStatusCode res >>= \res' ->
return (res')
getSlice :: List -> Integer -> Integer -> IO (List)
getSlice a1 a2 a3 =
withObject a1 $ \a1' ->
let {a2' = fromIntegral a2} in
let {a3' = fromIntegral a3} in
getSlice'_ a1' a2' a3' >>= \res ->
stealObject res >>= \res' ->
return (res')
setSlice
:: List
-> Integer
-> Integer
-> Maybe List
-> IO ()
setSlice self low high items = let
low' = fromIntegral low
high' = fromIntegral high in
withObject self $ \selfPtr ->
maybeWith withObject items $ \itemsPtr -> do
pyListSetSlice selfPtr low' high' itemsPtr
>>= checkStatusCode
sort :: List -> IO (())
sort a1 =
withObject a1 $ \a1' ->
sort'_ a1' >>= \res ->
checkStatusCode res >>= \res' ->
return (res')
reverse :: List -> IO (())
reverse a1 =
withObject a1 $ \a1' ->
reverse'_ a1' >>= \res ->
checkStatusCode res >>= \res' ->
return (res')
toTuple :: List -> IO (Tuple)
toTuple a1 =
withObject a1 $ \a1' ->
toTuple'_ a1' >>= \res ->
stealObject res >>= \res' ->
return (res')
foreign import ccall unsafe "CPython/Types/List.chs.h hscpython_PyList_Type"
listType'_ :: (Ptr ())
foreign import ccall safe "CPython/Types/List.chs.h hscpython_poke_list"
hscpython_poke_list :: (CUInt -> ((Ptr (Ptr ())) -> (IO (Ptr ()))))
foreign import ccall safe "CPython/Types/List.chs.h PyList_Size"
pyListSize :: ((Ptr ()) -> (IO CInt))
foreign import ccall safe "CPython/Types/List.chs.h hscpython_peek_list"
hscpython_peek_list :: ((Ptr ()) -> (CInt -> ((Ptr (Ptr ())) -> (IO ()))))
foreign import ccall safe "CPython/Types/List.chs.h PyList_Size"
length'_ :: ((Ptr ()) -> (IO CInt))
foreign import ccall safe "CPython/Types/List.chs.h PyList_GetItem"
getItem'_ :: ((Ptr ()) -> (CInt -> (IO (Ptr ()))))
foreign import ccall safe "CPython/Types/List.chs.h PyList_SetItem"
pyListSetItem :: ((Ptr ()) -> (CInt -> ((Ptr ()) -> (IO CInt))))
foreign import ccall safe "CPython/Types/List.chs.h PyList_Insert"
insert'_ :: ((Ptr ()) -> (CInt -> ((Ptr ()) -> (IO CInt))))
foreign import ccall safe "CPython/Types/List.chs.h PyList_Append"
append'_ :: ((Ptr ()) -> ((Ptr ()) -> (IO CInt)))
foreign import ccall safe "CPython/Types/List.chs.h PyList_GetSlice"
getSlice'_ :: ((Ptr ()) -> (CInt -> (CInt -> (IO (Ptr ())))))
foreign import ccall safe "CPython/Types/List.chs.h PyList_SetSlice"
pyListSetSlice :: ((Ptr ()) -> (CInt -> (CInt -> ((Ptr ()) -> (IO CInt)))))
foreign import ccall safe "CPython/Types/List.chs.h PyList_Sort"
sort'_ :: ((Ptr ()) -> (IO CInt))
foreign import ccall safe "CPython/Types/List.chs.h PyList_Reverse"
reverse'_ :: ((Ptr ()) -> (IO CInt))
foreign import ccall safe "CPython/Types/List.chs.h PyList_AsTuple"
toTuple'_ :: ((Ptr ()) -> (IO (Ptr ())))