{-# LINE 1 "lib/CPython/Protocols/Iterator.chs" #-}
{-# LANGUAGE ForeignFunctionInterface #-}
module CPython.Protocols.Iterator
( Iterator (..)
, SomeIterator
, castToIterator
, next
) where
import CPython.Internal
castToIterator :: Object a => a -> IO (Maybe SomeIterator)
castToIterator obj =
withObject obj $ \objPtr -> do
isIter <- fmap cToBool $ hscpythonPyIterCheck objPtr
return $ if isIter
then Just $ unsafeCastToIterator obj
else Nothing
next :: Iterator iter => iter -> IO (Maybe SomeObject)
next iter =
withObject iter $ \iterPtr -> do
raw <- pyIterNext iterPtr
if raw == nullPtr
then do
err <- pyErrOccurred
{-# LINE 47 "lib/CPython/Protocols/Iterator.chs" #-}
exceptionIf $ err /= nullPtr
return Nothing
else fmap Just $ stealObject raw
foreign import ccall safe "CPython/Protocols/Iterator.chs.h hscpython_PyIter_Check"
hscpythonPyIterCheck :: ((Ptr ()) -> (IO CInt))
foreign import ccall safe "CPython/Protocols/Iterator.chs.h PyIter_Next"
pyIterNext :: ((Ptr ()) -> (IO (Ptr ())))
foreign import ccall safe "CPython/Protocols/Iterator.chs.h PyErr_Occurred"
pyErrOccurred :: (IO (Ptr ()))