module Python.Interpreter (
py_initialize,
pyRun_SimpleString,
pyRun_String,
pyRun_StringHs,
StartFrom(..),
callByName,
callByNameHs,
noParms,
noKwParms,
pyImport,
pyImport_ImportModule,
pyImport_AddModule,
pyModule_GetDict,
py_initializeThreaded,
#ifndef PYTHON_PRE_2_3
cpy_GILEnsure,
cpy_GILRelease,
withGIL,
#endif
)
where
import Python.Utils (
checkCInt
, getDefaultGlobals
, withPyObject
, fromCPyObject
, py_incref
, pyModule_GetDict
, pyImport_AddModule
)
import Python.Objects (
toPyObject
, noParms
, noKwParms
, fromPyObject
, pyObject_Call
, pyObject_CallHs
, PyObject
, ToPyObject
, FromPyObject
)
import Python.Types (StartFrom(..))
import Python.ForeignImports (
cpy_initialize
, cpy_InitThreads
, cpyRun_String
, cpyRun_SimpleString
, cpyImport_ImportModuleEx
, sf2c
, pyImport_GetModuleDict
, pyDict_SetItemString
#ifndef PYTHON_PRE_2_3
, cpy_GILEnsure
, cpy_GILRelease
#endif
)
import Foreign.C (withCString)
py_initialize :: IO ()
py_initialize = do cpy_initialize
pyImport "traceback"
py_initializeThreaded :: IO ()
py_initializeThreaded = do cpy_InitThreads
py_initialize
#ifndef PYTHON_PRE_2_3
withGIL :: IO a -> IO a
withGIL act = do st <- cpy_GILEnsure
r <- act
cpy_GILRelease st
return r
#endif
pyRun_SimpleString :: String -> IO ()
pyRun_SimpleString x = withCString x (\cs ->
do cpyRun_SimpleString cs >>= checkCInt
return ()
)
pyRun_StringHs :: (ToPyObject b, FromPyObject c) =>
String
-> StartFrom
-> [(String, b)]
-> IO c
pyRun_StringHs cmd start locals =
let conv (k, v) = do v1 <- toPyObject v
return (k, v1)
in do
rlocals <- mapM conv locals
pyRun_String cmd start rlocals >>= fromPyObject
pyRun_String :: String
-> StartFrom
-> [(String, PyObject)]
-> IO PyObject
pyRun_String command startfrom xlocals =
let cstart = sf2c startfrom
in do dobj <- getDefaultGlobals
rlocals <- toPyObject xlocals
withCString command (\ccommand ->
withPyObject dobj (\cglobals ->
withPyObject rlocals (\clocals ->
cpyRun_String ccommand cstart cglobals clocals >>= fromCPyObject
)))
callByName :: String
-> [PyObject]
-> [(String, PyObject)]
-> IO PyObject
callByName fname sparms kwparms =
do func <- pyRun_String fname Py_eval_input []
pyObject_Call func sparms kwparms
callByNameHs :: (ToPyObject a, ToPyObject b, FromPyObject c) =>
String
-> [a]
-> [(String, b)]
-> IO c
callByNameHs fname sparms kwparms =
do func <- pyRun_String fname Py_eval_input []
pyObject_CallHs func sparms kwparms
pyImport :: String -> IO ()
pyImport x =
do pyImport_ImportModule x
globals <- getDefaultGlobals
cdict <- pyImport_GetModuleDict
py_incref cdict
pyo2 <- fromCPyObject cdict
dict <- fromPyObject pyo2
case lookup x dict of
Nothing -> return ()
Just pyo -> do withPyObject globals (\cglobals ->
withPyObject pyo (\cmodule ->
withCString x (\cstr ->
pyDict_SetItemString cglobals cstr cmodule >>= checkCInt)))
return ()
pyImport_ImportModule :: String -> IO PyObject
pyImport_ImportModule x =
do globals <- getDefaultGlobals
fromlist <- toPyObject ['*']
cr <- withPyObject globals (\cglobals ->
withPyObject fromlist (\cfromlist ->
withCString x (\cstr ->
cpyImport_ImportModuleEx cstr cglobals cglobals cfromlist)))
fromCPyObject cr