Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Python/Inline/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Python.Inline.Eval
, evalPyFunction
-- * Source code
, PyQuote(..)
, PyQuoteFun(..)
, Code
, codeFromText
, codeFromString
Expand Down
3 changes: 2 additions & 1 deletion src/Python/Inline/Monad/QQ.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Python.Inline.Monad.QQ
, pye
, pyf
, pycode
, pyfun
) where


Expand All @@ -17,7 +18,7 @@ import Language.Haskell.TH.Quote
import Python.Internal.EvalQQ
import Python.Internal.Eval
import Python.Inline.Monad
import Python.Inline.QQ (pycode)
import Python.Inline.QQ (pycode,pyfun)



Expand Down
15 changes: 15 additions & 0 deletions src/Python/Inline/QQ.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module Python.Inline.QQ
, pye
, pyf
, pycode
, pyfun
) where

import Language.Haskell.TH.Quote
Expand Down Expand Up @@ -122,3 +123,17 @@ pycode = QuasiQuoter
, quoteType = error "quoteType"
, quoteDec = error "quoteDec"
}

-- | Create quote of python code suitable for use with
-- 'Python.Inline.Eval.exec'
--
-- It creates value of type @PyQuoteFun@
--
-- @since 0.3@
pyfun :: QuasiQuoter
pyfun = QuasiQuoter
{ quoteExp = \txt -> expQQ Fun txt
, quotePat = error "quotePat"
, quoteType = error "quoteType"
, quoteDec = error "quoteDec"
}
6 changes: 3 additions & 3 deletions src/Python/Internal/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,10 +1104,10 @@ exec globals locals q = runProgram $ do
-- @since 0.3
evalPyFunction
:: Namespace global
=> global -- ^ Global variables
-> PyQuote -- ^ Source code
=> global -- ^ Global variables
-> PyQuoteFun -- ^ Source code
-> Py PyObject
evalPyFunction globals (PyQuote code binder) = runProgram $ do
evalPyFunction globals (PyQuoteFun code binder) = runProgram $ do
p_locals <- takeOwnership =<< progPy basicNewDict
p_kwargs <- takeOwnership =<< progPy basicNewDict
progPy $ do
Expand Down
7 changes: 4 additions & 3 deletions src/Python/Internal/EvalQQ.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
) where

import Control.Monad.IO.Class
import Control.Monad.Catch

Check warning on line 11 in src/Python/Internal/EvalQQ.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / GHC 9.6.6 / 3.12 /

The import of ‘Control.Monad.Catch’ is redundant

Check warning on line 11 in src/Python/Internal/EvalQQ.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / GHC 9.10.2 / 3.11 /

The import of ‘Control.Monad.Catch’ is redundant

Check warning on line 11 in src/Python/Internal/EvalQQ.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / GHC 9.10.2 / 3.12 /

The import of ‘Control.Monad.Catch’ is redundant

Check warning on line 11 in src/Python/Internal/EvalQQ.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / GHC 9.10.2 / 3.10 /

The import of ‘Control.Monad.Catch’ is redundant

Check warning on line 11 in src/Python/Internal/EvalQQ.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / GHC 9.14.1 / 3.12 /

The import of ‘Control.Monad.Catch’ is redundant

Check warning on line 11 in src/Python/Internal/EvalQQ.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / GHC 9.12.2 / 3.12 /

The import of ‘Control.Monad.Catch’ is redundant

Check warning on line 11 in src/Python/Internal/EvalQQ.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / GHC 9.8.4 / 3.12 /

The import of ‘Control.Monad.Catch’ is redundant

Check warning on line 11 in src/Python/Internal/EvalQQ.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / GHC 9.10.2 / 3.13 /

The import of ‘Control.Monad.Catch’ is redundant

Check warning on line 11 in src/Python/Internal/EvalQQ.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / GHC 9.10.2 / 3.14 /

The import of ‘Control.Monad.Catch’ is redundant

Check warning on line 11 in src/Python/Internal/EvalQQ.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / GHC 9.4.8 / 3.12 /

The import of ‘Control.Monad.Catch’ is redundant

Check warning on line 11 in src/Python/Internal/EvalQQ.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / GHC 9.10.2 / 3.12 / -fpython3-config

The import of ‘Control.Monad.Catch’ is redundant
import Data.Bits
import Data.Char
import Data.List (intercalate)
Expand All @@ -29,7 +29,7 @@
import Python.Internal.Types
import Python.Internal.Program
import Python.Internal.Eval
import Python.Internal.CAPI

Check warning on line 32 in src/Python/Internal/EvalQQ.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / GHC 9.6.6 / 3.12 /

The import of ‘Python.Internal.CAPI’ is redundant

Check warning on line 32 in src/Python/Internal/EvalQQ.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / GHC 9.10.2 / 3.11 /

The import of ‘Python.Internal.CAPI’ is redundant

Check warning on line 32 in src/Python/Internal/EvalQQ.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / GHC 9.10.2 / 3.12 /

The import of ‘Python.Internal.CAPI’ is redundant

Check warning on line 32 in src/Python/Internal/EvalQQ.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / GHC 9.10.2 / 3.10 /

The import of ‘Python.Internal.CAPI’ is redundant

Check warning on line 32 in src/Python/Internal/EvalQQ.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / GHC 9.14.1 / 3.12 /

The import of ‘Python.Internal.CAPI’ is redundant

Check warning on line 32 in src/Python/Internal/EvalQQ.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / GHC 9.12.2 / 3.12 /

The import of ‘Python.Internal.CAPI’ is redundant

Check warning on line 32 in src/Python/Internal/EvalQQ.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / GHC 9.8.4 / 3.12 /

The import of ‘Python.Internal.CAPI’ is redundant

Check warning on line 32 in src/Python/Internal/EvalQQ.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / GHC 9.10.2 / 3.13 /

The import of ‘Python.Internal.CAPI’ is redundant

Check warning on line 32 in src/Python/Internal/EvalQQ.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / GHC 9.10.2 / 3.14 /

The import of ‘Python.Internal.CAPI’ is redundant

Check warning on line 32 in src/Python/Internal/EvalQQ.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / GHC 9.4.8 / 3.12 /

The import of ‘Python.Internal.CAPI’ is redundant

Check warning on line 32 in src/Python/Internal/EvalQQ.hs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / GHC 9.10.2 / 3.12 / -fpython3-config

The import of ‘Python.Internal.CAPI’ is redundant
import Python.Inline.Literal


Expand Down Expand Up @@ -116,9 +116,10 @@
]
src_eval = prepareForEval mode antis src
--
[| PyQuote ($(TH.lift $ codeFromString src_eval))
(mconcat $(TH.listE args))
|]
case mode of
Eval -> [| PyQuote ($(TH.lift $ codeFromString src_eval)) (mconcat $(TH.listE args)) |]
Exec -> [| PyQuote ($(TH.lift $ codeFromString src_eval)) (mconcat $(TH.listE args)) |]
Fun -> [| PyQuoteFun ($(TH.lift $ codeFromString src_eval)) (mconcat $(TH.listE args)) |]


antiSuffix :: String
Expand Down
13 changes: 13 additions & 0 deletions src/Python/Internal/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module Python.Internal.Types
, pyIO
-- ** Python code wrappers
, PyQuote(..)
, PyQuoteFun(..)
, Code(..)
, codeFromText
, codeFromString
Expand Down Expand Up @@ -147,6 +148,18 @@ data PyQuote = PyQuote
}


-- | Quasiquoted body of python function which could be used by
-- 'Python.Inline.Eval.evalPyFunction'. Function body must be
-- wrapped in @_inline_python_@ function which quasiquoter
-- 'Python.Inline.QQ.pyfun' does automatically.
--
-- @since 0.3@
data PyQuoteFun = PyQuoteFun
{ code :: !Code
, binder :: !DictBinder
}


-- | UTF-8 encoded python source code.
--
-- @since 0.2@
Expand Down
4 changes: 4 additions & 0 deletions test/TST/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ testEvalExec = testGroup "eval/exec"
assert m_hs.a == 12
assert m_hs.b == 'asd'
|]
, testCase "evalPyFunction" $ runPy $ do
let n = 2 :: Int
x <- fromPy =<< evalPyFunction Temp [pyfun| return 100 * n_hs |]
liftIO $ Just (200::Int) @=? x
]

testAsync :: TestTree
Expand Down
Loading