{-# LINE 1 "lib/CPython/Types/ByteArray.chs" #-}
{-# LANGUAGE ForeignFunctionInterface #-}
module CPython.Types.ByteArray
( ByteArray
, byteArrayType
, toByteArray
, fromByteArray
, fromObject
, append
, length
, resize
) where
import qualified Foreign.C.Types as C2HSImp
import qualified Foreign.Ptr as C2HSImp
import qualified System.IO.Unsafe as C2HSImp
import Prelude hiding (length)
import qualified Data.ByteString as B
import qualified Data.ByteString.Unsafe as B
import CPython.Internal
newtype ByteArray = ByteArray (ForeignPtr ByteArray)
instance Object ByteArray where
toObject :: ByteArray -> SomeObject
toObject (ByteArray x :: ForeignPtr ByteArray
x) = ForeignPtr ByteArray -> SomeObject
forall a. Object a => ForeignPtr a -> SomeObject
SomeObject ForeignPtr ByteArray
x
fromForeignPtr :: ForeignPtr ByteArray -> ByteArray
fromForeignPtr = ForeignPtr ByteArray -> ByteArray
ByteArray
instance Concrete ByteArray where
concreteType :: ByteArray -> Type
concreteType _ = Type
byteArrayType
byteArrayType :: (Type)
byteArrayType =
C2HSImp.unsafePerformIO $
byteArrayType'_ IO (Ptr ()) -> (Ptr () -> IO Type) -> IO Type
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res :: Ptr ()
res ->
Ptr () -> IO Type
forall obj a. Object obj => Ptr a -> IO obj
peekStaticObject Ptr ()
res IO Type -> (Type -> IO Type) -> IO Type
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res' :: Type
res' ->
Type -> IO Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Type
res')
{-# LINE 47 "lib/CPython/Types/ByteArray.chs" #-}
toByteArray :: B.ByteString -> IO ByteArray
toByteArray bytes = let
mkByteArray = pyByteArrayFromStringAndSize
{-# LINE 51 "lib/CPython/Types/ByteArray.chs" #-}
in B.unsafeUseAsCStringLen bytes $ \(cstr, len) ->
stealObject =<< mkByteArray cstr (fromIntegral len)
fromByteArray :: ByteArray -> IO B.ByteString
fromByteArray py =
withObject py $ \pyPtr -> do
size' <- pyByteArraySize pyPtr
bytes <- pyByteArrayAsString pyPtr
B.packCStringLen (bytes, fromIntegral size')
fromObject :: Object self => (self) -> IO ((ByteArray))
fromObject :: self -> IO ByteArray
fromObject a1 :: self
a1 =
self -> (Ptr () -> IO ByteArray) -> IO ByteArray
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject self
a1 ((Ptr () -> IO ByteArray) -> IO ByteArray)
-> (Ptr () -> IO ByteArray) -> IO ByteArray
forall a b. (a -> b) -> a -> b
$ \a1' :: Ptr ()
a1' ->
Ptr () -> IO (Ptr ())
fromObject'_ Ptr ()
a1' IO (Ptr ()) -> (Ptr () -> IO ByteArray) -> IO ByteArray
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res :: Ptr ()
res ->
Ptr () -> IO ByteArray
forall obj a. Object obj => Ptr a -> IO obj
stealObject Ptr ()
res IO ByteArray -> (ByteArray -> IO ByteArray) -> IO ByteArray
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res' :: ByteArray
res' ->
ByteArray -> IO ByteArray
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteArray
res')
{-# LINE 67 "lib/CPython/Types/ByteArray.chs" #-}
append :: (ByteArray) -> (ByteArray) -> IO ((ByteArray))
append a1 a2 =
withObject a1 $ \a1' ->
withObject a2 $ \a2' ->
append'_ a1' a2' >>= \res ->
stealObject res >>= \res' ->
return (res')
{-# LINE 72 "lib/CPython/Types/ByteArray.chs" #-}
length :: (ByteArray) -> IO ((Integer))
length a1 =
withObject a1 $ \a1' ->
length'_ a1' >>= \res ->
checkIntReturn res >>= \res' ->
return (res')
{-# LINE 76 "lib/CPython/Types/ByteArray.chs" #-}
resize :: (ByteArray) -> (Integer) -> IO ((()))
resize a1 a2 =
withObject a1 $ \a1' ->
let {a2' = fromIntegral a2} in
resize'_ a1' a2' >>= \res ->
checkStatusCode res >>= \res' ->
return (res')
{-# LINE 81 "lib/CPython/Types/ByteArray.chs" #-}
foreign import ccall unsafe "CPython/Types/ByteArray.chs.h hscpython_PyByteArray_Type"
byteArrayType'_ :: (IO (C2HSImp.Ptr ()))
foreign import ccall safe "CPython/Types/ByteArray.chs.h PyByteArray_FromStringAndSize"
pyByteArrayFromStringAndSize :: ((C2HSImp.Ptr C2HSImp.CChar) -> (C2HSImp.CLong -> (IO (C2HSImp.Ptr ()))))
foreign import ccall safe "CPython/Types/ByteArray.chs.h PyByteArray_Size"
pyByteArraySize :: ((C2HSImp.Ptr ()) -> (IO C2HSImp.CLong))
foreign import ccall safe "CPython/Types/ByteArray.chs.h PyByteArray_AsString"
pyByteArrayAsString :: ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr C2HSImp.CChar)))
foreign import ccall safe "CPython/Types/ByteArray.chs.h PyByteArray_FromObject"
fromObject'_ :: ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr ())))
foreign import ccall safe "CPython/Types/ByteArray.chs.h PyByteArray_Concat"
append'_ :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr ()))))
foreign import ccall safe "CPython/Types/ByteArray.chs.h PyByteArray_Size"
length'_ :: ((C2HSImp.Ptr ()) -> (IO C2HSImp.CLong))
foreign import ccall safe "CPython/Types/ByteArray.chs.h PyByteArray_Resize"
resize'_ :: ((C2HSImp.Ptr ()) -> (C2HSImp.CLong -> (IO C2HSImp.CInt)))