From 05d7bc556ff51f191683006b0f1be3716df58128 Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Tue, 11 Apr 2017 21:46:33 -0400 Subject: [PATCH] Initialize Python interpreter in PythonImpl constructor --- src/PYTHON/python_impl.cpp | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/src/PYTHON/python_impl.cpp b/src/PYTHON/python_impl.cpp index d9abb75f13..0c671c0cc7 100644 --- a/src/PYTHON/python_impl.cpp +++ b/src/PYTHON/python_impl.cpp @@ -45,14 +45,26 @@ enum{NONE,INT,DOUBLE,STRING,PTR}; PythonImpl::PythonImpl(LAMMPS *lmp) : Pointers(lmp) { - pyMain = NULL; - // pfuncs stores interface info for each Python function nfunc = 0; pfuncs = NULL; - external_interpreter = false; + // one-time initialization of Python interpreter + // pymain stores pointer to main module + external_interpreter = Py_IsInitialized(); + + Py_Initialize(); + PyEval_InitThreads(); + + PyGILState_STATE gstate = PyGILState_Ensure(); + + PyObject *pModule = PyImport_AddModule("__main__"); + if (!pModule) error->all(FLERR,"Could not initialize embedded Python"); + + pyMain = (void *) pModule; + + PyGILState_Release(gstate); } /* ---------------------------------------------------------------------- */ @@ -168,22 +180,7 @@ void PythonImpl::command(int narg, char **arg) int ifunc = create_entry(arg[0]); - // one-time initialization of Python interpreter - // pymain stores pointer to main module - PyGILState_STATE gstate; - - if (pyMain == NULL) { - external_interpreter = Py_IsInitialized(); - Py_Initialize(); - PyEval_InitThreads(); - gstate = PyGILState_Ensure(); - - PyObject *pModule = PyImport_AddModule("__main__"); - if (!pModule) error->all(FLERR,"Could not initialize embedded Python"); - pyMain = (void *) pModule; - } else { - gstate = PyGILState_Ensure(); - } + PyGILState_STATE gstate = PyGILState_Ensure(); // send Python code to Python interpreter // file: read the file via PyRun_SimpleFile()