From c4337243b0b2334ef1ef13c98f9dc18eb28b23ac Mon Sep 17 00:00:00 2001 From: Alexey Khudyakov Date: Tue, 29 Sep 2026 13:11:41 +0300 Subject: [PATCH] Add eval/exec variants which use MonadPy --- src/Python/Inline/Monad.hs | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/Python/Inline/Monad.hs b/src/Python/Inline/Monad.hs index 190b0a5..e6f261c 100644 --- a/src/Python/Inline/Monad.hs +++ b/src/Python/Inline/Monad.hs @@ -3,6 +3,9 @@ module Python.Inline.Monad ( -- * Type classes Namespace(..) , MonadPy(..) + , evalM + , execM + , evalPyFunctionM ) where import Python.Internal.Eval @@ -13,6 +16,8 @@ import Python.Internal.Types -- wrapper around 'Py'. -- -- For @Py@ global variables are 'Main' and local are 'Temp' +-- +-- @since 0.3 class Monad m => MonadPy m where -- | Lift @Py@ computation into given monad. liftPy :: Py a -> m a @@ -31,3 +36,41 @@ instance MonadPy Py where liftPy = id withGlobals f = f Main withLocals f = f Temp + + +-- | Analog of 'eval' which uses globals and locals carried by 'MonadPy' +-- +-- @since 0.3 +evalM + :: (MonadPy m) + => PyQuote -- ^ Source code + -> m PyObject +evalM q = + withGlobals $ \globals -> + withLocals $ \locals -> + liftPy $ eval globals locals q + +-- | Analog of 'exec' which uses globals and locals carried by +-- 'MonadPy' +-- +-- @since 0.3 +execM + :: (MonadPy m) + => PyQuote -- ^ Source code + -> m () +execM q = + withGlobals $ \globals -> + withLocals $ \locals -> + liftPy $ exec globals locals q + +-- | Analog of 'evalPyFunction' which uses globals and locals carried by +-- 'MonadPy'. +-- +-- @since 0.3 +evalPyFunctionM + :: (MonadPy m) + => PyQuote -- ^ Source code + -> m PyObject +evalPyFunctionM q = + withGlobals $ \globals -> + liftPy $ evalPyFunction globals q