{-# LINE 1 "lib/CPython/Types/Capsule.chs" #-}
{-# LANGUAGE ForeignFunctionInterface #-}
module CPython.Types.Capsule
( Capsule
, capsuleType
, getPointer
, getContext
, getName
, importNamed
, isValid
, setPointer
, setContext
) where
import Data.Text (Text)
import CPython.Internal hiding (new)
newtype Capsule = Capsule (ForeignPtr Capsule)
instance Object Capsule where
toObject (Capsule x) = SomeObject x
fromForeignPtr = Capsule
instance Concrete Capsule where
concreteType _ = capsuleType
capsuleType :: (Type)
capsuleType =
unsafePerformIO $
let {res = capsuleType'_} in
peekStaticObject res >>= \res' ->
return (res')
{-# LINE 51 "lib/CPython/Types/Capsule.chs" #-}
getPointer :: Capsule -> Maybe Text -> IO (Ptr ())
getPointer py name =
withObject py $ \pyPtr ->
maybeWith withText name $ \namePtr ->
pyCapsuleGetPointer pyPtr namePtr
getContext :: Capsule -> IO (Ptr ())
getContext py =
withObject py $ \pyPtr -> do
pyErrClear
{-# LINE 76 "lib/CPython/Types/Capsule.chs" #-}
ptr <- pyCapsuleGetContext pyPtr
if ptr /= nullPtr
then return ptr
else do
exc <- pyErrOccurred
{-# LINE 81 "lib/CPython/Types/Capsule.chs" #-}
exceptionIf $ exc /= nullPtr
return ptr
getName :: Capsule -> IO (Maybe Text)
getName py =
withObject py $ \pyPtr -> do
pyErrClear
{-# LINE 89 "lib/CPython/Types/Capsule.chs" #-}
ptr <- pyCapsuleGetName pyPtr
if ptr /= nullPtr
then Just `fmap` peekText ptr
else do
exc <- pyErrOccurred
{-# LINE 94 "lib/CPython/Types/Capsule.chs" #-}
exceptionIf $ exc /= nullPtr
return Nothing
importNamed :: Text -> Bool -> IO (Maybe (Ptr ()))
importNamed name block =
withText name $ \namePtr ->
let noBlock = cFromBool (not block) in do
pyErrClear
{-# LINE 112 "lib/CPython/Types/Capsule.chs" #-}
ptr <- pyCapsuleImport namePtr noBlock
if ptr /= nullPtr
then return $ Just ptr
else do
exc <- pyErrOccurred
{-# LINE 117 "lib/CPython/Types/Capsule.chs" #-}
exceptionIf $ exc /= nullPtr
return Nothing
isValid :: Capsule -> Maybe Text -> IO Bool
isValid py name =
withObject py $ \pyPtr ->
maybeWith withText name $ \namePtr ->
pyCapsuleIsValid pyPtr namePtr
>>= checkBoolReturn
setPointer :: (Capsule) -> (Ptr ()) -> IO ((()))
setPointer a1 a2 =
withObject a1 $ \a1' ->
let {a2' = id a2} in
setPointer'_ a1' a2' >>= \res ->
checkStatusCode res >>= \res' ->
return (res')
{-# LINE 139 "lib/CPython/Types/Capsule.chs" #-}
setContext :: (Capsule) -> (Ptr ()) -> IO ((()))
setContext a1 a2 =
withObject a1 $ \a1' ->
let {a2' = id a2} in
setContext'_ a1' a2' >>= \res ->
checkStatusCode res >>= \res' ->
return (res')
{-# LINE 148 "lib/CPython/Types/Capsule.chs" #-}
foreign import ccall unsafe "CPython/Types/Capsule.chs.h hscpython_PyCapsule_Type"
capsuleType'_ :: (Ptr ())
foreign import ccall safe "CPython/Types/Capsule.chs.h PyCapsule_GetPointer"
pyCapsuleGetPointer :: ((Ptr ()) -> ((Ptr CChar) -> (IO (Ptr ()))))
foreign import ccall safe "CPython/Types/Capsule.chs.h PyErr_Clear"
pyErrClear :: (IO ())
foreign import ccall safe "CPython/Types/Capsule.chs.h PyCapsule_GetContext"
pyCapsuleGetContext :: ((Ptr ()) -> (IO (Ptr ())))
foreign import ccall safe "CPython/Types/Capsule.chs.h PyErr_Occurred"
pyErrOccurred :: (IO (Ptr ()))
foreign import ccall safe "CPython/Types/Capsule.chs.h PyCapsule_GetName"
pyCapsuleGetName :: ((Ptr ()) -> (IO (Ptr CChar)))
foreign import ccall safe "CPython/Types/Capsule.chs.h PyCapsule_Import"
pyCapsuleImport :: ((Ptr CChar) -> (CInt -> (IO (Ptr ()))))
foreign import ccall safe "CPython/Types/Capsule.chs.h PyCapsule_IsValid"
pyCapsuleIsValid :: ((Ptr ()) -> ((Ptr CChar) -> (IO CInt)))
foreign import ccall safe "CPython/Types/Capsule.chs.h PyCapsule_SetPointer"
setPointer'_ :: ((Ptr ()) -> ((Ptr ()) -> (IO CInt)))
foreign import ccall safe "CPython/Types/Capsule.chs.h PyCapsule_SetContext"
setContext'_ :: ((Ptr ()) -> ((Ptr ()) -> (IO CInt)))