{-# LINE 1 "lib/CPython/Types/ByteArray.chs" #-}
{-# LANGUAGE ForeignFunctionInterface #-}
module CPython.Types.ByteArray
( ByteArray
, byteArrayType
, toByteArray
, fromByteArray
, fromObject
, append
, length
, resize
) where
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 x) = SomeObject x
fromForeignPtr = ByteArray
instance Concrete ByteArray where
concreteType _ = byteArrayType
byteArrayType :: (Type)
byteArrayType =
unsafePerformIO $
let {res = byteArrayType'_} in
peekStaticObject res >>= \res' ->
return (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 a1 =
withObject a1 $ \a1' ->
fromObject'_ a1' >>= \res ->
stealObject res >>= \res' ->
return (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'_ :: (Ptr ())
foreign import ccall safe "CPython/Types/ByteArray.chs.h PyByteArray_FromStringAndSize"
pyByteArrayFromStringAndSize :: ((Ptr CChar) -> (CLong -> (IO (Ptr ()))))
foreign import ccall safe "CPython/Types/ByteArray.chs.h PyByteArray_Size"
pyByteArraySize :: ((Ptr ()) -> (IO CLong))
foreign import ccall safe "CPython/Types/ByteArray.chs.h PyByteArray_AsString"
pyByteArrayAsString :: ((Ptr ()) -> (IO (Ptr CChar)))
foreign import ccall safe "CPython/Types/ByteArray.chs.h PyByteArray_FromObject"
fromObject'_ :: ((Ptr ()) -> (IO (Ptr ())))
foreign import ccall safe "CPython/Types/ByteArray.chs.h PyByteArray_Concat"
append'_ :: ((Ptr ()) -> ((Ptr ()) -> (IO (Ptr ()))))
foreign import ccall safe "CPython/Types/ByteArray.chs.h PyByteArray_Size"
length'_ :: ((Ptr ()) -> (IO CLong))
foreign import ccall safe "CPython/Types/ByteArray.chs.h PyByteArray_Resize"
resize'_ :: ((Ptr ()) -> (CLong -> (IO CInt)))