@@ -103,7 +103,8 @@ class PythonEnvironmentData
103103
104104 std::set<std::string> addedPath;
105105
106- py::module m_sofamodule ;
106+ py::module m_sofaModule ;
107+ py::module m_sofaRuntimeModule ;
107108private:
108109 std::vector<wchar_t *> m_argv;
109110};
@@ -180,27 +181,26 @@ void PythonEnvironment::Init()
180181 }
181182
182183 PyEval_InitThreads ();
183- gil lock;
184184
185185 // Required for sys.path, used in addPythonModulePath().
186- PyRun_SimpleString (" import sys" );
186+ executePython ([]{ PyRun_SimpleString (" import sys" );} );
187187
188188 // Force C locale.
189- PyRun_SimpleString (" import locale" );
190- PyRun_SimpleString (" locale.setlocale(locale.LC_ALL, 'C')" );
189+ executePython ([]{ PyRun_SimpleString (" import locale" );} );
190+ executePython ([]{ PyRun_SimpleString (" locale.setlocale(locale.LC_ALL, 'C')" );} );
191191
192192 // Workaround: try to import numpy and to launch numpy.finfo to cache data;
193193 // this prevents a deadlock when calling numpy.finfo from a worker thread.
194- PyRun_SimpleString (" try:\n\t import numpy;numpy.finfo(float)\n except:\n\t pass" );
194+ executePython ([]{ PyRun_SimpleString (" try:\n\t import numpy;numpy.finfo(float)\n except:\n\t pass" );} );
195195
196196 // Workaround: try to import scipy from the main thread this prevents a deadlock when importing
197197 // scipy from a worker thread when we use the SofaScene asynchronous loading
198- PyRun_SimpleString (" try:\n\t from scipy import misc, optimize\n except:\n\t pass\n " );
198+ executePython ([]{ PyRun_SimpleString (" try:\n\t from scipy import misc, optimize\n except:\n\t pass\n " );} );
199199
200200 // If the script directory is not available (e.g. if the interpreter is invoked interactively
201201 // or if the script is read from standard input), path[0] is the empty string,
202202 // which directs Python to search modules in the current directory first.
203- PyRun_SimpleString (std::string (" sys.path.insert(0,\"\" )" ).c_str ());
203+ executePython ([]{ PyRun_SimpleString (std::string (" sys.path.insert(0,\"\" )" ).c_str ());} );
204204
205205 // Add the paths to the plugins' python modules to sys.path. Those paths
206206 // are read from all the files in 'etc/sofa/python.d'
@@ -237,11 +237,12 @@ void PythonEnvironment::Init()
237237 // Lastly, we (try to) add modules from the root of SOFA
238238 addPythonModulePathsFromDirectory ( Utils::getSofaPathPrefix () );
239239
240- getStaticData ()->m_sofamodule = py::module::import (" Sofa" );
241- PyRun_SimpleString (" import SofaRuntime" );
240+ executePython ([]{ getStaticData ()->m_sofaModule = py::module::import (" Sofa" ); });
241+ executePython ([]{ getStaticData ()->m_sofaRuntimeModule = py::module::import (" SofaRuntime" ); });
242+ executePython ([]{ PyRun_SimpleString (" import SofaRuntime" );});
242243
243244 // python livecoding related
244- PyRun_SimpleString (" from Sofa.livecoding import onReimpAFile" );
245+ executePython ([]{ PyRun_SimpleString (" from Sofa.livecoding import onReimpAFile" );} );
245246
246247 // general sofa-python stuff
247248
@@ -311,16 +312,14 @@ void PythonEnvironment::Release()
311312void PythonEnvironment::addPythonModulePath (const std::string& path)
312313{
313314 PythonEnvironmentData* data = getStaticData () ;
314- if ( data->addedPath .find (path)==data->addedPath .end ()) {
315+ if ( data->addedPath .find (path)==data->addedPath .end ())
316+ {
315317 // note not to insert at first 0 place
316318 // an empty string must be at first so modules can be found in the current directory first.
317319
318- {
319- gil lock;
320- PyRun_SimpleString (std::string (" sys.path.insert(1,\" " +path+" \" )" ).c_str ());
321- }
320+ executePython ([&]{ PyRun_SimpleString (std::string (" sys.path.insert(1,\" " +path+" \" )" ).c_str ());});
322321
323- msg_info (" SofaPython3" ) << " Added '" + path + " ' to sys.path" ;
322+ msg_info (" SofaPython3" )<< " Added '" + path + " ' to sys.path" ;
324323 data->addedPath .insert (path);
325324 }
326325}
@@ -508,22 +507,15 @@ std::string PythonEnvironment::getStackAsString()
508507
509508std::string PythonEnvironment::getPythonCallingPointString ()
510509{
511- return py::cast<std::string>(getStaticData ()->m_sofamodule .attr (" getPythonCallingPointAsString" )());
510+ gil lock;
511+ return py::cast<std::string>(getStaticData ()->m_sofaModule .attr (" getPythonCallingPointAsString" )());
512512}
513513
514514sofa::helper::logging::FileInfo::SPtr PythonEnvironment::getPythonCallingPointAsFileInfo ()
515515{
516- PyObject* pDict = PyModule_GetDict (PyImport_AddModule (" SofaRuntime" ));
517- PyObject* res = PyDict_GetItemString (pDict, " getPythonCallingPoint" );
518- if (res && PySequence_Check (res) ){
519- PyObject* filename = PySequence_GetItem (res, 0 ) ;
520- PyObject* number = PySequence_GetItem (res, 1 ) ;
521- std::string tmp=PyBytes_AsString (filename);
522- auto lineno = PyLong_AsLong (number);
523- Py_DECREF (res) ;
524- return SOFA_FILE_INFO_COPIED_FROM (tmp, lineno);
525- }
526- return SOFA_FILE_INFO_COPIED_FROM (" undefined" , -1 );
516+ gil lock;
517+ py::tuple cp = getStaticData ()->m_sofaRuntimeModule .attr (" getPythonCallingPoint" )();
518+ return SOFA_FILE_INFO_COPIED_FROM (py::cast<std::string>(cp[0 ]), py::cast<int >(cp[1 ]));
527519}
528520
529521void PythonEnvironment::setArguments (const std::string& filename, const std::vector<std::string>& arguments)
@@ -546,9 +538,8 @@ void PythonEnvironment::setArguments(const std::string& filename, const std::vec
546538
547539void PythonEnvironment::SceneLoaderListerner::rightBeforeLoadingScene ()
548540{
549- gil lock;
550541 // unload python modules to force importing their eventual modifications
551- PyRun_SimpleString (" SofaRuntime.unloadModules()" );
542+ executePython ([]{ PyRun_SimpleString (" SofaRuntime.unloadModules()" );} );
552543}
553544
554545void PythonEnvironment::setAutomaticModuleReload ( bool b )
@@ -561,8 +552,7 @@ void PythonEnvironment::setAutomaticModuleReload( bool b )
561552
562553void PythonEnvironment::excludeModuleFromReload ( const std::string& moduleName )
563554{
564- gil lock;
565- PyRun_SimpleString ( std::string ( " try: SofaRuntime.__SofaPythonEnvironment_modulesExcludedFromReload.append('" + moduleName + " ')\n except:pass" ).c_str () );
555+ executePython ([&]{ PyRun_SimpleString ( std::string ( " try: SofaRuntime.__SofaPythonEnvironment_modulesExcludedFromReload.append('" + moduleName + " ')\n except:pass" ).c_str () );});
566556}
567557
568558static const bool debug_gil = false ;
0 commit comments