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')
toByteArray :: B.ByteString -> IO ByteArray
toByteArray bytes = let
mkByteArray = pyByteArrayFromStringAndSize
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')
append :: ByteArray -> ByteArray -> IO (ByteArray)
append a1 a2 =
withObject a1 $ \a1' ->
withObject a2 $ \a2' ->
append'_ a1' a2' >>= \res ->
stealObject res >>= \res' ->
return (res')
length :: ByteArray -> IO (Integer)
length a1 =
withObject a1 $ \a1' ->
length'_ a1' >>= \res ->
checkIntReturn res >>= \res' ->
return (res')
resize :: ByteArray -> Integer -> IO (())
resize a1 a2 =
withObject a1 $ \a1' ->
let {a2' = fromIntegral a2} in
resize'_ a1' a2' >>= \res ->
checkStatusCode res >>= \res' ->
return (res')
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) -> (CInt -> (IO (Ptr ()))))
foreign import ccall safe "CPython/Types/ByteArray.chs.h PyByteArray_Size"
pyByteArraySize :: ((Ptr ()) -> (IO CInt))
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 CInt))
foreign import ccall safe "CPython/Types/ByteArray.chs.h PyByteArray_Resize"
resize'_ :: ((Ptr ()) -> (CInt -> (IO CInt)))