@@ -181,27 +181,26 @@ void PythonEnvironment::Init()
181181 }
182182
183183 PyEval_InitThreads ();
184- gil lock;
185184
186185 // Required for sys.path, used in addPythonModulePath().
187- PyRun_SimpleString (" import sys" );
186+ executePython ([]{ PyRun_SimpleString (" import sys" );} );
188187
189188 // Force C locale.
190- PyRun_SimpleString (" import locale" );
191- PyRun_SimpleString (" locale.setlocale(locale.LC_ALL, 'C')" );
189+ executePython ([]{ PyRun_SimpleString (" import locale" );} );
190+ executePython ([]{ PyRun_SimpleString (" locale.setlocale(locale.LC_ALL, 'C')" );} );
192191
193192 // Workaround: try to import numpy and to launch numpy.finfo to cache data;
194193 // this prevents a deadlock when calling numpy.finfo from a worker thread.
195- 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" );} );
196195
197196 // Workaround: try to import scipy from the main thread this prevents a deadlock when importing
198197 // scipy from a worker thread when we use the SofaScene asynchronous loading
199- 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 " );} );
200199
201200 // If the script directory is not available (e.g. if the interpreter is invoked interactively
202201 // or if the script is read from standard input), path[0] is the empty string,
203202 // which directs Python to search modules in the current directory first.
204- PyRun_SimpleString (std::string (" sys.path.insert(0,\"\" )" ).c_str ());
203+ executePython ([]{ PyRun_SimpleString (std::string (" sys.path.insert(0,\"\" )" ).c_str ());} );
205204
206205 // Add the paths to the plugins' python modules to sys.path. Those paths
207206 // are read from all the files in 'etc/sofa/python.d'
@@ -238,12 +237,12 @@ void PythonEnvironment::Init()
238237 // Lastly, we (try to) add modules from the root of SOFA
239238 addPythonModulePathsFromDirectory ( Utils::getSofaPathPrefix () );
240239
241- getStaticData ()->m_sofaModule = py::module::import (" Sofa" );
242- getStaticData ()->m_sofaRuntimeModule = py::module::import (" SofaRuntime" );
243- 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" );} );
244243
245244 // python livecoding related
246- PyRun_SimpleString (" from Sofa.livecoding import onReimpAFile" );
245+ executePython ([]{ PyRun_SimpleString (" from Sofa.livecoding import onReimpAFile" );} );
247246
248247 // general sofa-python stuff
249248
@@ -313,16 +312,14 @@ void PythonEnvironment::Release()
313312void PythonEnvironment::addPythonModulePath (const std::string& path)
314313{
315314 PythonEnvironmentData* data = getStaticData () ;
316- if ( data->addedPath .find (path)==data->addedPath .end ()) {
315+ if ( data->addedPath .find (path)==data->addedPath .end ())
316+ {
317317 // note not to insert at first 0 place
318318 // an empty string must be at first so modules can be found in the current directory first.
319319
320- {
321- gil lock;
322- PyRun_SimpleString (std::string (" sys.path.insert(1,\" " +path+" \" )" ).c_str ());
323- }
320+ executePython ([&]{ PyRun_SimpleString (std::string (" sys.path.insert(1,\" " +path+" \" )" ).c_str ());});
324321
325- msg_info (" SofaPython3" ) << " Added '" + path + " ' to sys.path" ;
322+ msg_info (" SofaPython3" )<< " Added '" + path + " ' to sys.path" ;
326323 data->addedPath .insert (path);
327324 }
328325}
@@ -541,9 +538,8 @@ void PythonEnvironment::setArguments(const std::string& filename, const std::vec
541538
542539void PythonEnvironment::SceneLoaderListerner::rightBeforeLoadingScene ()
543540{
544- gil lock;
545541 // unload python modules to force importing their eventual modifications
546- PyRun_SimpleString (" SofaRuntime.unloadModules()" );
542+ executePython ([]{ PyRun_SimpleString (" SofaRuntime.unloadModules()" );} );
547543}
548544
549545void PythonEnvironment::setAutomaticModuleReload ( bool b )
@@ -556,8 +552,7 @@ void PythonEnvironment::setAutomaticModuleReload( bool b )
556552
557553void PythonEnvironment::excludeModuleFromReload ( const std::string& moduleName )
558554{
559- gil lock;
560- 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 () );});
561556}
562557
563558static const bool debug_gil = false ;
0 commit comments